JAVA中字符串函數subString的用法小結。本站提示廣大學習愛好者:(JAVA中字符串函數subString的用法小結)文章只能為提供參考,不一定能成為您想要的結果。以下是JAVA中字符串函數subString的用法小結正文
String str;
str=str.substring(int beginIndex);截取失落str從首字母起長度為beginIndex的字符串,將殘剩字符串賦值給str;
str=str.substring(int beginIndex,int endIndex);截取str中從beginIndex開端至endIndex停止時的字符串,並將其賦值給str;
demo:
class Test
{
public static void main(String[] args)
{
String s1 ="1234567890abcdefgh";
s1 = s1.substring(10);
System.out.println(s1);
}
}
運轉成果:abcdefgh
class Test
{
public static void main(String[] args)
{
String s1 ="1234567890abcdefgh";
s1 = s1.substring(0,9);
System.out.println(s1);
}
}
運轉成果:123456789
上面是個典范例子:
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);
}
}
運轉成果:
this is my string