Split string into characters array in Java Android

 How to Separate-divide string variable value into char array with comma and show on screen using TextView.

In this tutorial we are splitting string data type variable value to char array which is already automatically separate the string value into char set with comma one by one. So here is the complete step by step tutorial for Split string into characters array in Java Android.

android-project-download-code-button

How to Split string into characters array in Java Android.

Code for MainActivity.java file.

 package com.splitstringintochararray_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview1,textview2;
 
 //Initializing string variable.
 String value = "VALUE" ;
 
 //Initializing Char array variable.
 char[] charArray;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview1 = (TextView)findViewById(R.id.textView1);
 textview2 = (TextView)findViewById(R.id.textView2);
 
 //Converting string variable to byte array.
 charArray = value.toCharArray();
 
 //printing string on screen.
 
 textview1.setText("String data = " + value);
 
 //Printing Char array on screen.
 
 textview2.setText("Char Array = ");
 
 for(int i=0;i<charArray.length;i++){
 
 textview2.setText(textview2.getText() +""+ charArray[i] + ", ");
 }
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.splitstringintochararray_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:text="String"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView1"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="56dp"
 android:text="Char"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshot:

Split string into characters array in Java Android

Click here to download Split string into characters array in Java Android project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *