java中使用String的split分隔字符串注意事项

在使用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     }

运行结果为:

 

posted @ 2020-08-15 13:24  Joker-0927  阅读(202)  评论(0编辑  收藏  举报
// 侧边栏目录