Summary of the use of the string function subString in JAVA
- 2020-04-01 02:52:37
- OfStack
String STR.
STR = STR. The substring (int beginIndex); Intercept the string whose length is beginIndex from the first letter of STR, and assign the remaining string to STR;
STR = STR. Substring (int beginIndex, int endIndex); Intercepts the string from beginIndex to endIndex in STR and assigns it to STR.
The demo:
class Test
{
public static void main(String[] args)
{
String s1 ="1234567890abcdefgh";
s1 = s1.substring(10);
System.out.println(s1);
}
}
Result: abcdefgh
class Test
{
public static void main(String[] args)
{
String s1 ="1234567890abcdefgh";
s1 = s1.substring(0,9);
System.out.println(s1);
}
}
Results: 123456789
Here's a typical example:
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);
}
}
Operation results:
This is my string