手机
当前位置:查字典教程网 >编程开发 >Java >JAVA中字符串函数subString的用法小结
JAVA中字符串函数subString的用法小结
摘要:Stringstr;str=str.substring(intbeginIndex);截取掉str从首字母起长度为beginIndex的字符...

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中字符串函数subString的用法小结】相关文章:

关于JAVA中this的使用方法小结

java中的快捷键小结

Java字符串详解的实例介绍

Java instanceof 运算符的使用方法

JAVA中 终止线程的方法介绍

java中计算字符串长度的方法及u4E00与u9FBB的认识

JAVA中STRING的常用方法小结

Java中去除字符串中所有空格的几种方法

JAVA 多态 由浅及深介绍

浅析JAVA中toString方法的作用

精品推荐
分类导航