字符串分割,你会吗?

 

      对于字符串的分割主要有两种方式,使用Stringsplit方法和使用StringTokenizer。现在假设我们有一个字符串“||a|b||c|d|||”,然后我们需要使用“|”来对它进行分割。那么如下两段代码你觉得输出结果会是什么呢?

    public static void main(String args[]) {
        String str = "||a|b||c|d|||";
        String strArray[] = str.split("\\|");
        System.out.println(strArray.length);
        for (int i=0; i<strArray.length; i++) {
            System.out.println("strArray["+i+"] = " + strArray[i]);
        }
    }
 
    public static void main(String args[]) {
        String str = "||a|b||c|d|||";
        StringTokenizer tokenizer = new StringTokenizer(str, "|");
        int len = tokenizer.countTokens();
        System.out.println(len);
        while (tokenizer.hasMoreTokens()) {
            System.out.println(tokenizer.nextToken());
        }
    }

 

 

 

      正确答案是第一段代码会输出如下:

7
strArray[0] = 
strArray[1] = 
strArray[2] = a
strArray[3] = b
strArray[4] = 
strArray[5] = c
strArray[6] = d

 

 

 

      第二段代码输出如下:

4
a
b
c
d

 

       下面这段代码呢?

    public static void main(String args[]) {
        String str = "|||abcd||eacd|||";
        StringTokenizer tokenizer = new StringTokenizer(str, "|ab");
        int len = tokenizer.countTokens();
        System.out.println(len);
        while (tokenizer.hasMoreTokens()) {
            System.out.println(tokenizer.nextToken());
        }
    }

       输出如下:

3
cd
e
cd

 

      你答对了吗?接下来我们来解释一下为什么是上面的输出结果。

      Stringsplit方法分割

Stringsplit方法接收的参数是一个正则表达式,其底层调用的是正则表达式的split方法。Stringsplit方法源码是这样的:

 

    public String[] split(String regex, int limit) {
            return Pattern.compile(regex).split(this, limit);
    }

 

 

 

 

 

      而正则表达式Patternsplit方法是这样实现的:

    public String[] split(CharSequence input, int limit) {
        int index = 0;
        boolean matchLimited = limit > 0;
        ArrayList<String> matchList = new ArrayList<String>();
        Matcher m = matcher(input);
 
        // Add segments before each match found
        while(m.find()) {
            if (!matchLimited || matchList.size() < limit - 1) {
                String match = input.subSequence(index, m.start()).toString();
                matchList.add(match);
                index = m.end();
            } else if (matchList.size() == limit - 1) { // last one
                String match = input.subSequence(index,
                                                 input.length()).toString();
                matchList.add(match);
                index = m.end();
            }
        }
 
        // If no match was found, return this
        if (index == 0)
            return new String[] {input.toString()};
 
        // Add remaining segment
        if (!matchLimited || matchList.size() < limit)
            matchList.add(input.subSequence(index, input.length()).toString());
 
        // Construct result
        int resultSize = matchList.size();
        if (limit == 0)
            while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
                resultSize--;
        String[] result = new String[resultSize];
        return matchList.subList(0, resultSize).toArray(result);
    }

 

 

 

      当分割的字符串中含有分隔符时,注意这段代码:

        int resultSize = matchList.size();
        if (limit == 0)
            while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
                resultSize--;
        String[] result = new String[resultSize];
        return matchList.subList(0, resultSize).toArray(result);    

 

在没有限制分割数量的情况下,Pattern会把最末端为空串的元素都丢弃。所以当我们使用split方法以“|”为分隔符分割字符串||a|b||c|d|||”时将会把最后三个空元素丢弃,也就出现了上面的结果。

StringTokenizer分割

       StringTokenizer会把传递的分隔符拆分成一个个的字符,也就是char,然后会把每一个char都当成是一个分隔符。在进行字符串分割时会判断字符串的每一个字符是否在分隔符中,如果在则跳过这个字符,直到遇见不是分隔符的字符,然后把连续不是分隔符的字符组成一个串,如此往复。所以当我们使用“|”分割字符串"||a|b||c|d|||"时,StringTokenizer遇见分隔符“|”就跳过了,就会出现上述的结果。StringTokenizer的源码太多了我这里就不贴了,有兴趣的朋友可以自己去看。同样的道理,当我们使用“ab|”分割字符串“|||abcd||eacdf|||”时会把它分割成3个字符串,分别是“cd”、“e”、“cdf”。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值