Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract substring from a string

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

like image 550
Moe Avatar asked Mar 24 '11 04:03

Moe


People also ask

How can you extract a substring from a given string?

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.

What substring () and substr () will do?

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 .


1 Answers

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); 
like image 146
Kartik Domadiya Avatar answered Sep 29 '22 23:09

Kartik Domadiya