Extract substring from string

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

+53
android string substring
Mar 24 2018-11-11T00:
source share
8 answers

If you know the start and end index, you can use

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

If you want to get a substring from a specific index to the end, you can use:

 String substr=mysourcestring.substring(startIndex); 

If you want to get a substring from a specific character to the end, you can use:

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

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

 String substr=mysourcestring.substring(mysourcestring.indexOf("characterValue") + 1); 
+96
Mar 24 '11 at 4:19
source share

substring() :

 str.substring(startIndex, endIndex); 
+71
Mar 24 '11 at 4:13
source share

Here is an example of the real world:

 String hallostring = "hallo"; String asubstring = hallostring.substring(0, 1); 

In the asubstring example, it will return: h

+19
Oct 30 '13 at 15:06
source share

There is another way if you want to get a substring before and after the character

 String s ="123dance456"; String[] split = s.split("dance"); String firstSubString = split[0]; String secondSubString = split[1]; 

how to find before and after a substring in a string

+5
Jun 14 '17 at 9:24
source share

use text unold class from android:
TextUtils.substring (charsequence source, int start, int end)

+3
Sep 27 '13 at 7:11
source share

You can use subSequence, same as substr in C

  Str.subSequence(int Start , int End) 
+3
Oct 08 '13 at 17:14
source share

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 of type "." and startString as "_". therefore, this function returns "AMANDROID". enjoy the code time. :)

+2
Aug 25 '15 at 6:53
source share

The best way to get a substring in Android is to use (as @ user2503849 said) the TextUtlis.substring(CharSequence, int, int) method. I can explain why. If you look at the String.substring(int, int) method from android.jar (latest 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); } 

Well, than ... What do you think the private constructor of 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 refers to the array of "old" value char[] . Therefore, the GC cannot release it.

In the latest 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 internal copying of the array internally.

What is it:)

Respectfully!

0
Jul 22 '15 at 12:12
source share



All Articles