How to use substring in Java
str=str.substring (int beginindex), intercept str from the initial letter length of Beginindex string, the remaining string is assigned to STR;
str=str.substring (int beginindex,int endIndex), intercepts the string from Beginindex to EndIndex at the end of STR and assigns it to STR;
Here is a demo program:
public class stringdemo{
public static void Main (String agrs[]) {
String Str= "This is my original string";
String todelete= "original";
if (Str.startswith (Todelete))
Str=str.substring (Todelete.length ());
Else
if (Str.endswith (Todelete))
Str=str.substring (0, Str.length ()-todelete.length ());
Else
{
int Index=str.indexof (todelete);
if (index!=-1)
{
String str1=str.substring (0, index);
String str2=str.substring (Index+todelete.length ());
STR=STR1+STR2;
}
Else
System.out.println ("string/" "+todelete+"/"Not Found");
}
System.out.println (str);
}
}
(Original quote from: http://hi.baidu.com/ccsos/blog/item/42ff84afe6e62bcd7dd92a62.html)
Add: str=str.substring (int beginindex,int endIndex); The value that is finally obtained:
Beginindex =< str Values < EndIndex
The above supplement is a bit of my own understanding of the recent comments in the API to see it, put it in the following so many others and I just started to learn better understanding of the above procedure substring
substring (int beginindex, int endIndex)
-
Returns a new string that is a substring of this string. The substring starts at the specified
Beginindex
and continues to the character at the index
endIndex-1
. Therefore, the length of the substring is
endindex-beginindex
.
Demo Sample:
"Hamburger". Substring (4, 8) returns "Urge" "smiles". Substring (1, 5) returns "Mile"
-
-
-
Number:
-
beginindex
-the index (inclusive) at the beginning.
-
endIndex
-the index at the end (not included).
-
Returns:
-
the specified substring.
-
Thrown:
-
indexoutofboundsexception
-assuming
beginindex
endIndex is greater than the length of this
String
object, or
beginindex
is greater than
EndIndex
.