Str = str. substring (int beginIndex); truncates the string whose length is beginIndex from the first letter, and assigns the remaining string to str;
Str = str. substring (int beginIndex, int endIndex); truncate the string from beginIndex to endIndex in str and assign it to str;
Instance
Public class Utils {
Public static String diff (String str1, String str2 ){
Int index = str1.lastIndexOf (str2 );
If (index>-1 ){
Return str1.substring (str2.length ());
}
Return str1;
}
}
Instance 2
Public class TestSubstring {
Public static void main (String [] args ){
String str1 = "fghjkl ";
String str2 = str1.substring (1); // truncate the String starting from position 1st, cut it to the end, and assign the return value to str2.
System. out. println ("str1 =" + str1 );
System. out. println ("str2 =" + str2 );
}
}
Instance 3
Public static String substring (String str, int start, int end ){
If (str = null ){
Return null;
}
// Handle negatives
If (end <0 ){
End = str. length () + end; // remember end is negative
}
If (start <0 ){
Start = str. length () + start; // remember start is negative
}
// Check length next
If (end> str. length ()){
End = str. length ();
}
// If start is greater than end, return ""
If (start> end ){
Return "";
}
If (start <0 ){
Start = 0;
}
If (end <0 ){
End = 0;
}
Return str. substring (start, end );
}
}