您的位置:首页 > 其它

split切割字符串注意事项

2017-09-11 17:20 267 查看
split()方法的源码:

public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/
char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
int off = 0;
int next = 0;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
while ((next = indexOf(ch, off)) != -1) {
if (!limited || list.size() < limit - 1) {
list.add(substring(off, next));
off = next + 1;
} else {    // last one
//assert (list.size() == limit - 1);
list.add(substring(off, value.length));
off = value.length;
break;
}
}
// If no match was found, return this
if (off == 0)
return new String[]{this};

// Add remaining segment
if (!limited || list.size() < limit)
list.add(substring(off, value.length));

// Construct result
int resultSize = list.size();
if (limit == 0)
while (resultSize > 0 && list.get(resultSize - 1).length() == 0)
resultSize--;
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}
从源码中可以看出,split()方法是正则表达式,所以如果要使用到以下字符“.$|()[{^?*+\\”作为切割符的话,需要进行转义,因为这些字符在正则表达式中属于一种有特殊含义的字符。
*********************************************************************************************************************************************************

【1】单个符号作为分隔符 
       String address="上海|上海市|闵行区|吴中路";
     String[] splitAddress=address.split("\\|"); //如果以竖线为分隔符,则split的时候需要加上两个斜杠【\\】进行转义
     System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);

    

         String address="上海*上海市*闵行区*吴中路";

     String[] splitAddress=address.split("\\*");

     System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);

    

        String address="上海:上海市:闵行区:吴中路";
     String[] splitAddress=address.split("\\:");
     System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);

         String address="上海.上海市.闵行区.吴中路";
     String[] splitAddress=address.split("\\.");
     System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
    

       String address="上海^上海市^闵行区^吴中路";
     String[] splitAddress=address.split("\\^");
     System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
    

         String address="上海@上海市@闵行区@吴中路";
     String[] splitAddress=address.split("@");
     System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);

    

    String address="上海,上海市,闵行区,吴中路";
     String[] splitAddress=address.split(",");
     System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);

    【2】多个符号作为分隔符

      String address="上海^上海市@闵行区#吴中路";
     String[] splitAddress=address.split("\\^|@|#");
     System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);
    

***************************格式 小提示***************************************************

 String address = new String("192.168.13.240");
        String[] str = address.split("\\.");
        for(String s : str){
            System.out.println(s);
        }

输出格式:
192
168
13
240
System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]);

输出格式:上海上海市闵行区吴中路
*****************************************************************

总结:(1)split表达式,其实就是一个正则表达式。*  ^ | 等符号在正则表达式中属于一种有特殊含义的字符,如果使用此种字符作为分隔符,必须使用转义符即\\加以转义。

          (2)如果使用多个分隔符则需要借助 | 符号,如【2】所示,但需要转义符的仍然要加上分隔符进行处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  split