的子串()方法返回其对应于原始字符串开始从索引开始,直到最后一个索引字符串的数据类型。如果未指定结束索引,则必须使endIndex为字符串长度。由于我们正在处理String,因此索引从' 0'位置开始。
语法public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
beginIndex:我们要开始对String进行剪切或子串化的起始索引或位置。
endIndex:我们要结束切割或将String子串化的结束索引或位置。
此方法返回String数据类型,该数据类型对应于我们剪切的字符串部分。如果未指定endIndex,则假定结束索引为String长度-1,如果beginIndex为负或大于String的长度,则抛出IndexOutOfBoundsException。
示例public class StringSubstringTest{
public static void main(String[] args) {
String str = "Welcome to nhooo.com";
System.out.println(str.substring(5));
System.out.println(str.substring(2, 5));
str.substring(6);
System.out.println("str value: "+ str);
String str1 = str.substring(5);
System.out.println("str1 value: "+ str1);
}
}
输出结果me to nhooo.com
lco
str value: Welcome to nhooo.com
str1 value: me to nhooo.com