public class SubstringExample {
public static void main(String[] args) {
String str = "Hello, Java!";
// 截取从索引3开始到索引7的子字符串(包括索引3,不包括索引7)
String sub1 = str.substring(3, 7);
System.out.println("substring(3, 7): " + sub1); // 输出: lo,
// 截取从索引5开始到最后的子字符串
String sub2 = str.substring(5);
System.out.println("substring(5): " + sub2); // 输出: , Java!
}
}
str.substring(int beginIndex, int endIndex):此方法用于截取从beginIndex开始到endIndex-1之间的子字符串。即,包含beginIndex位置的字符,但不包含endIndex位置的字符。str.substring(int beginIndex):此方法用于截取从beginIndex开始到字符串末尾的子字符串。以上代码展示了如何使用substring方法来截取字符串的不同部分。
上一篇:java判断字符串包含某个字符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站