How to split String before first comma?

Santosh Bhandary picture Santosh Bhandary · Jun 2, 2015 · Viewed 61.7k times · Source

I have an overriding method with String which returns String in format of:

 "abc,cde,def,fgh"

I want to split the string content into two parts:

  1. String before first comma and

  2. String after first comma

My overriding method is :

@Override
protected void onPostExecute(String addressText) {

    placeTitle.setText(addressText);
}

Now how do I split the string into two parts, so that I can use them to set the text in two different TextView?

Answer

Razib picture Razib · Jun 2, 2015

You may use the following code snippet

String str ="abc,cde,def,fgh";
String kept = str.substring( 0, str.indexOf(","));
String remainder = str.substring(str.indexOf(",")+1, str.length());