源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JAVA中字符串函数subString的用法小结

  • 时间:2020-06-05 07:54 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JAVA中字符串函数subString的用法小结
[b]String str; [/b]str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str; str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str; [b]demo: [/b]
[u]复制代码[/u] 代码如下:
class Test {  public static void main(String[] args)  {   String s1 ="1234567890abcdefgh";   s1 = s1.substring(10);   System.out.println(s1);  } }
运行结果:abcdefgh
[u]复制代码[/u] 代码如下:
class Test {  public static void main(String[] args)  {   String s1 ="1234567890abcdefgh";   s1 = s1.substring(0,9);   System.out.println(s1);  } }
运行结果:123456789 下面是个典型例子:
[u]复制代码[/u] 代码如下:
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
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部