字符串分割
1.使用String类中的split()方法可以对字符串进行分割,split()方法提供了以下两种字符串的分割形式:
(1)split(**regex:**使用“具体分割条件”进行分割 **limit:**分割后结果分成几份)
案例:
public static void main(String[] args) {
String s = "123,456,789";
//使用split进行字符串分割,使用“,”进行分割
// String[] split = s.split(",");
// String s1 = ArrayUtils.toString(split);
// System.out.println(s1);
//使用split进行字符串分割,(regex:使用“,”进行分割 limit:分割后结果分成2份)
String[] split = s.split(",", 2);
for (String s1 : split) {
System.out.println(s1);
}
System.out.println(split.length);
}
运行结果
[INFO] --- exec-maven-plugin:1.6.0:exec (default-cli) @ test ---
123
456,789
2
字符串截取
-
语法:字符串.substring(开始下标,结束下标) - 返回被截取的字符串,不包含结束下标对应的字符
-
不可以使用负数表示下标
案例:
public static void main(String[] args) {
//字符串截取
String str = "abcdefg";
String substring = str.substring(1, 3);
System.out.println(substring);
}
运行结果
[INFO] --- exec-maven-plugin:1.6.0:exec (default-cli) @ test ---
bc