Skip to main content

java - How to split string into array of character strings


import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        // Set background color for action bar
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));

        // Get widget reference from XML layout
        TextView tv = (TextView) findViewById(;

        // Initialize a new string variable
        String alphabet = "ABCD";

        // Display the string
        tv.setText("String : " + alphabet);

        // Split the String and generate a String array
        String[] splitString = alphabet.split("");

        tv.setText(tv.getText() + "\nString array length : " + splitString.length);
        tv.setText(tv.getText() + "\nString array (split) : ");
        // Iterate over the String array
        for(int i=0;i<splitString.length;i++){
            tv.setText(tv.getText() +""+ splitString[i] + ", ");

        // Perfect way to split a string to array of character strings
        // String split by using regular expression
        String[] letters = alphabet.split("(?!^)");

        tv.setText(tv.getText() + "\n\nCorrect way to split string to letters");
        tv.setText(tv.getText() + "\nString array length : " + letters.length);
        tv.setText(tv.getText() + "\nString array (split - regular expression) : ");
        // Iterate over the String array
        for(int i=0;i<letters.length;i++){
            tv.setText(tv.getText() +""+ letters[i] + ", ");
More android examples

Popular posts from this blog

Android Popup Window Example

activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" xmlns:tools="" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#f5f1e0" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Popup Window" /> </RelativeLayout> res/layout/custom_layout.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" android:id="@+id/rl_custom_layout" …

How to add a hint to Spinner in Android

activity_main.xml <RelativeLayout xmlns:android="" xmlns:tools="" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#60c4ff" > <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> spinner_item.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textStyle="italic" …

How to create a round/circle Button in Android

activity_main.xml code snippet <Button android:id ="@+id/push_button" android:layout_width="150dp" android:layout_height="150dp" android:text="Round Button" android:background="@drawable/button_bg_round" android:padding="15dp" /> res/drawable/button_bg_round.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android=""> <item> <shape android:shape="oval"> <stroke android:color="#1E90FF" android:width="5dp" /> <solid android:color="#87CEEB"/> <size android:width="150dp" android:height="150dp"/> </shape> </item> </selector> More android examples How to set Button fontHow to change Button font programmaticallyHow to set Button text style to bold and italic…