1、 public String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串(sub)。该子字符串始于指定索引处的字符,一直到此字符串末尾。(从0开始数)
如果 beginIndex 为负或大于此 String 对象的长度,则显示错误
例 如 :String A=“ilovecpu”
System.out.print(A.substring(3));
输出为:vecpu
2、public String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始, endIndex:到指定的 endIndex-1处结束。
参数:beginIndex - 开始处的索引(包括)
endindex 结尾处索引(不包括)。
返回:指定的子字符串
例如:
String A=“ilovecpu”
System.out.print(A.substring(1,5));
输出为:love
来个例题测试一下吧
运行如下程序,输出结果是()。
StringBuffer sb = new StringBuffer("good morning!");
String sub = sb.substring(0, 8);
System.out.println(sub);
System.out.print("/");
char c = sb.charAt(6);
System.out.println(c);
A.good mor
/o
B.good morn/o
C.good morn/m
D.good mor/
o
正确答案:A