Ad

Split String Into 2 Sub Strings On First Occurrence Of Comma?

- 1 answer

I am trying to split String in to 2 sub_strings on the first appearance of comma character.

Also the first occurred comma must be removed.

Example :

Suppose this is the string

Manhattan Ave, Brooklyn,NY,USA

dividing this into this

Manhattan Ave

Brooklyn,NY,USA

also to note that first comma has been removed.

and finally saving these sub_string into variables.

String Place = "Manhattan Ave, Brooklyn,NY,USA";
 String result_1 = Place.substring(0, Place.indexOf('.'));
 String result_2 ="";
Ad

Answer

You can try this,

String place = "Manhattan Ave, Brooklyn,NY,USA";
int index = place.indexOf(',');
String result1 = place.substring(0,index).trim();
String result2 = place.substring(index+1).trim();
Ad
source: stackoverflow.com
Ad