How to split a String in ANDROID?

By | May 5, 2011


Hi all…..
Splitting a string in android is really easy.checkout the following snippet.

package com.coderzheaven;

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

public class SplitStringDemo extends Activity {
    String str = "India, USA, Japan, Russia, France";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String arr[] = str.split(",");
        for(int i = 0; i < arr.length; i++){
        	System.out.println("arr["+i+"] = " + arr[i].trim());
        }
    }
}

Check the Logcat for output.
Note: Make sure to put the escape character before the delimiter for splitting,otherwise the output may be each character.

Leave a Reply

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