Santosh Bhandary Santosh Bhandary - 5 months ago 17
Java Question

How to split String before first comma?

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

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