字符串的截取
(1)substring(int beginIndex);
(2)substring(int beginIndex, int endIndex);
下面展示一些 内联代码片
。
//字符串截取、提取
public static void main(String[] args) {
String str = "Today is a sunny day!";
String s = "我喜欢autumn";
//截取从0开始的字符串部分
System.out.print(str.substring(0));
//截取[ 0,5)的字符串部分,左闭右开
System.out.print(str.substring(0,5));
//按字符截取,不是按字节截取
System.out.print(s.substring(3,9));
}
字符串的分割
下面展示一些 内联代码片
。
//字符串分割
public static void main(String[] args) {
String str = "com.L.String | xiaoL";
String s[] = str.split("\\.|\\|");//"."、"|"是转义字符,需要加"\\"
String ss[] = str.split("L");
for(int i = 0; i < s.length; i++)
System.out.print(s[i] + "\t");
for(int i = 0; i < ss.length; i++)
System.out.print(ss[i]);
}