字符串分割,你会吗?

简介:         对于字符串的分割主要有两种方式,使用String的split方法和使用StringTokenizer。现在假设我们有一个字符串“||a|b||c|d|||”,然后我们需要使用“|”来对它进行分割。

 

      对于字符串的分割主要有两种方式,使用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”。

 

目录
相关文章
|
测试技术 索引
根据首尾字符串截取中间字符串
今天分享一个函数:虽然它非常简单,但是真的很好用!也很常用!比如 “我今天真的很高兴” 这句话,要把 `今天` 截取出来,我们可以直接调用函数拿到结果,不需要匹配索引、也不用写正则!
69 0
|
索引
分割字符串的方法
分割字符串的方法
88 0
字符串截取
字符串截取
50 0
字符串的倒序与大小写转换
字符串的倒序与大小写转换
105 0
|
JavaScript 前端开发 索引
查找字符串中的字符串
查找字符串中的字符串
77 0
|
机器学习/深度学习 NoSQL 算法
字符串——344.反转字符串
本专栏按照数组—链表—哈希—字符串—栈与队列—二叉树—回溯—贪心—动态规划—单调栈的顺序刷题,采用代码随想录所给的刷题顺序,一个正确的刷题顺序对算法学习是非常重要的,希望对大家有帮助
字符串——344.反转字符串
|
开发者 Python
字符串分割相关的方法|学习笔记
快速学习字符串分割相关的方法
字符串——541. 反转字符串 II
本专栏按照数组—链表—哈希—字符串—栈与队列—二叉树—回溯—贪心—动态规划—单调栈的顺序刷题,采用代码随想录所给的刷题顺序,一个正确的刷题顺序对算法学习是非常重要的,希望对大家有帮助
|
C++
C++常用字符串分割方法
来源:http://www.jb51.net/article/55954.htm 一、用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串。 参数说明:str为要分解的字符串,delim为分隔符字符串。 返回值:从str开头开始的一个个被分
6484 0