- 字符串截取从索引0开始,包含头不包含尾,如果方法传入一个单独的索引,例如2,那么得到的结果是从索引2开始后面的所有字符.
- 如果方法传入的是一个索引范围(2,4),那么得到的结果是从索引2到索引4的前一位.具体案例看下列代码演示.
public void Demo(){
String str = "helloword";
System.out.println(str.substring(1));
System.out.println(str.substring(3));
System.out.println(str.substring(2,4));
System.out.println(str.substring(0,4));
}
上面代码运行得到的结果分别是:
elloword
loword
ll
hell
本文深入讲解了字符串截取的方法,包括从指定位置开始截取及指定范围内的截取技巧,通过实例代码演示了如何在实际编程中应用这些方法。
262

被折叠的 条评论
为什么被折叠?



