what is the best way to extract a substring from a string in android?
You can extract a substring from a String using the substring() method of the String class to this method you need to pass the start and end indexes of the required substring.
The difference between substring() and substr() The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With