Extract substring from a string

Moe picture Moe · Mar 24, 2011 · Viewed 229.2k times · Source

what is the best way to extract a substring from a string in android?

Answer

Kartik Domadiya picture Kartik Domadiya · Mar 24, 2011

If you know the Start and End index, you can use

String substr=mysourcestring.substring(startIndex,endIndex);

If you want to get substring from specific index till end you can use :

String substr=mysourcestring.substring(startIndex);

If you want to get substring from specific character till end you can use :

String substr=mysourcestring.substring(mysourcestring.indexOf("characterValue"));

If you want to get substring from after a specific character, add that number to .indexOf(char):

String substr=mysourcestring.substring(mysourcestring.indexOf("characterValue") + 1);