How to split String before first comma?

String splitted[] =s.split(",",2); // will be matched 1 times. 

splitted[0]  //before the first comma. `abc`
splitted[1]  //the whole String after the first comma. `cde,def,fgh`

If you want only cde as the string after first comma. Then you can use

String splitted[] =s.split(",",3); // will be matched  2 times

or without the limit

String splitted[] =s.split(",");

Don't forget to check the length to avoid ArrayIndexOutOfBound.


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