String str;
str=str.substring(int beginIndex); intercept the string with the length beginIndex from the first letter, and assign the remaining string to str;
str=str.substring(int beginIndex, int endIndex); intercept the string in str from beginIndex to endIndex, and assign it to str;
demo:
The code copy is as follows:
class Test
{
public static void main(String[] args)
{
String s1 ="1234567890abcdefgh";
s1 = s1.substring(10);
System.out.println(s1);
}
}
Running result: abcdefgh
The code copy is as follows:
class Test
{
public static void main(String[] args)
{
String s1 ="1234567890abcdefgh";
s1 = s1.substring(0,9);
System.out.println(s1);
}
}
Running result: 123456789
Here is a typical example:
The code copy is as follows:
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);
}
}
Running results:
This is my string