what is the best way to extract a substring from a string in android?
11 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);

- 3,592
- 1
- 29
- 50

- 29,868
- 19
- 93
- 104
-
suppose i want the index of some character in my string i can use .indexof("char"). But in my situation sometimes that character might not be in the string. then i want to get an empty string. how to handle this error? – Abhinav Raja Jun 25 '14 at 23:32
-
Just test the return value first. Something like: if (str.indexOf("char") > 0) – Chuck Claunch Nov 11 '14 at 19:00
-
Great answer, but substring needs to be all lowercase. So it's substring() not subString() – CodyMace Feb 06 '15 at 16:45
-
The Android documentation is a better source: String substring (int beginIndex, int endIndex) Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. – Martin Sep 21 '17 at 07:17
Here is a real world example:
String hallostring = "hallo";
String asubstring = hallostring.substring(0, 1);
In the example asubstring would return: h

- 988
- 1
- 10
- 21
-
6This answer implicitly adds the information missing from the other two more popular answers that the string is 0, not 1 based, and deserves more love than it is getting for doing so. – LDMJoe Mar 27 '16 at 00:09
-
2It is not the base which is important. substring takes a partial string starting at position startIndex and extending up to but not including endIndex. – Martin Sep 21 '17 at 07:21
-
2Base not important??? It's mind-boggling vital to know that indices run from zero to string.length(), and that the last character returned is the one indexed by endIndex-1, wouldn't you say? – kbro Jul 16 '19 at 14:24
-
This is still the best answer because it shows how beginIndex is 0-based and endIndex is 1-based. Because as bizarre as that sounds, that is how this function works. – ATL Jan 18 '23 at 03:00
There is another way , if you want to get sub string before and after a character
String s ="123dance456";
String[] split = s.split("dance");
String firstSubString = split[0];
String secondSubString = split[1];
check this post- how to find before and after sub-string in a string

- 360
- 3
- 12
substring(int startIndex, int endIndex)
If you don't specify endIndex, the method will return all the characters from startIndex.
startIndex : starting index is inclusive
endIndex : ending index is exclusive
Example:
String str = "abcdefgh"
str.substring(0, 4)
=> abcd
str.substring(4, 6)
=> ef
str.substring(6)
=> gh

- 343
- 4
- 15
-
1The definition (0, 4) leads abcd which is the best answer! Will (0,0) or (1,1) show the result "a" only? – Bay Sep 10 '19 at 13:02
-
you can use this code
public static String getSubString(String mainString, String lastString, String startString) {
String endString = "";
int endIndex = mainString.indexOf(lastString);
int startIndex = mainString.indexOf(startString);
Log.d("message", "" + mainString.substring(startIndex, endIndex));
endString = mainString.substring(startIndex, endIndex);
return endString;
}
in this mainString
is a Super string.like
"I_AmANDROID.Devloper"
and lastString
is a string like"." and startString is like"_".
so this function
returns "AmANDROID".
enjoy your code time.:)

- 1,781
- 17
- 27
use text untold class from android:
TextUtils.substring (charsequence source, int start, int end)

- 2,418
- 3
- 19
- 23

- 110
- 12
You can use subSequence , it's same as substr in C
Str.subSequence(int Start , int End)

- 743
- 6
- 3
-
substr in C++ is defined with START and LENGTH, not startIndex and endIndex: string substr (size_t pos = 0, size_t len = npos) const; – Martin Sep 21 '17 at 07:24
When finding multiple occurrences of a substring matching a pattern
String input_string = "foo/adsfasdf/adf/bar/erqwer/";
String regex = "(foo/|bar/)"; // Matches 'foo/' and 'bar/'
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input_string);
while(matcher.find()) {
String str_matched = input_string.substring(matcher.start(), matcher.end());
// Do something with a match found
}

- 399
- 3
- 6
The best way to get substring in Android is using (as @user2503849 said) TextUtlis.substring(CharSequence, int, int)
method. I can explain why. If you will take a look at the String.substring(int, int)
method from android.jar
(newest API 22), you will see:
public String substring(int start) {
if (start == 0) {
return this;
}
if (start >= 0 && start <= count) {
return new String(offset + start, count - start, value);
}
throw indexAndLength(start);
}
Ok, than... How do you think the private constructor String(int, int, char[])
looks like?
String(int offset, int charCount, char[] chars) {
this.value = chars;
this.offset = offset;
this.count = charCount;
}
As we can see it keeps reference to the "old" value char[]
array. So, the GC can not free it.
In the newest Java it was fixed:
String(int offset, int charCount, char[] chars) {
this.value = Arrays.copyOfRange(chars, offset, offset + charCount);
this.offset = offset;
this.count = charCount;
}
Arrays.copyOfRange(...)
uses native array copying inside.
That's it :)
Best regards!

- 796
- 7
- 16
All of The responders gave good answers. However, I am giving you all relatable methods for this so that any one can get from one place, I'll edit my answer if I find something new.
substring(0)- use for cut string from given specific char.
Substring(0,2)- give you sub string from starting(0) and ending(2) characters.
Split("NAME")- return you string in two parts first is that you use in split "NAME" and another part is rest of string combine.
subSequence(0,3) - returns sequence of give start(0) and ending index(3).
This one is not specifically use for split string but though it may be use full for some one
startswith("A",3)- returns string for specific starting character.
For example:
String s = "StackOverflow"; String[] split = s.split("Stack"); System.out.println("STRING NAME:"+s.substring(2)); System.out.println("STRING NAME:"+s.substring(2,3)); System.out.println("STRING NAME:"+split[1]); System.out.println("STRING NAME:"+split[0]); System.out.println("STRING NAME:"+s.subSequence(2,5));
Output:
1)ackOverflow 2)a 3)Overflow 4)stack 5)ack
I hope this will give you enough information that you require.

- 732,580
- 175
- 1,330
- 1,459

- 1
- 2