在使用split方法对字符串进行分割的时候,有些标点符号不能直接当做分割的对象:
比如:* ^ : | , . 需要加上"\"
1 public static void main(String[] args) { 2 String s="1.2.3.4.5"; 3 String[] split = s.split("."); 4 for (String s1 : split) { 5 System.out.println(s1); 6 } 7 System.out.println("数组的长度是:"+split.length); 8 9 10 }
运行结果为:
加上转义字符后:
1 public static void main(String[] args) { 2 String s="1.2.3.4.5"; 3 String[] split = s.split("\."); 4 for (String s1 : split) { 5 System.out.println(s1); 6 } 7 System.out.println("数组的长度是:"+split.length); 8 9 10 }
运行结果为: