Java – How to split String before first comma

delimiterjavastring

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?

Best Solution

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());