如何在Java中使用java.lang.String类的substring()方法?
的子串()方法返回其对应于原始字符串开始从索引开始,直到最后一个索引字符串的数据类型。如果未指定结束索引,则必须使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
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短