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
相關文章
利用Java Apache POI 生成Word文檔示例代碼
本篇文章主要介紹了利用Java Apache POI 生成Word文檔示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Java開發(fā)druid數據連接池maven方式簡易配置流程示例
本篇文章主要為大家介紹了java開發(fā)中druid數據連接池maven方式的簡易配置流程示例,文中附含詳細的代碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10SpringBoot學習篇之@Valid與@Validated的區(qū)別
@Valid是使用Hibernate?validation的時候使用,@Validated是只用Spring?Validator校驗機制使用,下面這篇文章主要給大家介紹了關于SpringBoot學習篇之@Valid與@Validated區(qū)別的相關資料,需要的朋友可以參考下2022-11-11