从一个字符串中可以取出指定的子字符串,称为字符串的截取操作。
| 方法名称{普通方法} | 描述 |
|---|---|
| public String substring(int beginIndex){普通方法} | 从指定索引截取到结尾 |
| public String substring(int beginIndex,int endIndex) {普通方法} | 截取指定索范围中的子字符串 |
范例:字符串截取
public class StringDemo155 {
public static void main(String[] args) {
String str="java-lll-se";
System.out.println(str.substring(4));//指定位置截取到结尾
System.out.println(str.substring(5,8));//截取指定索引范围的子字符串
}
}
-lll-se
lll
范例:通过字符串索引确定截取范围
public class StringDemo15502 {
public static void main(String[] args) {
String str="qqw-e.reg-rntjun-jncu";
int beg=str.indexOf("-",str.indexOf("e.reg"));//开始索引
int end=str.lastIndexOf("-");
System.out.println(str.substring(beg+1,end));
}
}
rntjun
1180

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



