简体   繁体   English

Java String split()方法的奇怪行为

[英]Strange behavior of Java String split() method

I have a method which takes a string parameter and split the string by # and after splitting it prints the length of the array along with array elements. 我有一个采用字符串参数并用#分割字符串的方法,分割后将打印数组的长度以及数组元素。 Below is my code 下面是我的代码

public void StringSplitTesting(String inputString) {

        String tokenArray[] = inputString.split("#");

        System.out.println("tokenArray length is " + tokenArray.length
                + " and array elements are " + Arrays.toString(tokenArray));

    }

Case I : Now when my input is abc# the output is tokenArray length is 1 and array elements are [abc] 情况I:现在,当我的输入为abc#时 ,输出为tokenArray length is 1 and array elements are [abc]

Case II : But when my input is #abc the output is tokenArray length is 2 and array elements are [, abc] 情况二:但是当我的输入为#abc时 ,输出为tokenArray length is 2 and array elements are [, abc]

But I was expecting the same output for both the cases. 但是我期望两种情况下的输出相同。 What is the reason behind this implementation? 此实现背后的原因是什么? Why split() method is behaving like this? 为什么split()方法的行为如此? Could someone give me proper explanation on this? 有人可以给我适当的解释吗?

One aspect of the behavior of the one-argument split method can be surprising -- trailing nulls are discarded from the returned array. 参数split方法的行为的一个方面可能令人惊讶-从返回的数组中丢弃尾随的null。

Trailing empty strings are therefore not included in the resulting array. 因此,结尾的空字符串不包括在结果数组中。

To get a length of 2 for each case, you can pass in a negative second argument to the two-argument split method , which means that the length is unrestricted and no trailing empty strings are discarded. 要使每种情况的长度为2 ,可以将负的第二个参数传递给double 参数的split方法 ,这意味着该长度是不受限制的,并且不会丢弃任何尾随的空字符串。

Just take a look in the documentation: 只需看一下文档:

Trailing empty strings are therefore not included in the resulting array. 因此,结尾的空字符串不包括在结果数组中。

So in case 1, the output would be {"abc", ""} but Java cuts the trailing empty String. 因此,在情况1中,输出为{"abc", ""}但是Java剪切了结尾的空String。 If you don't want the trailing empty String to be discarded, you have to use split("#", -1) . 如果您不希望尾随的空String被丢弃,则必须使用split("#", -1)

The observed behavior is due to the inherently asymmetric nature of the substring() method in Java: 观察到的行为归因于Java中substring()方法的固有不对称特性:

This is the core of the implementation of split() : 这是split()实现的核心:

         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;
            }
        }

The key to understanding the behavior of the above code is to understand the behavior of the substring() method: 理解上述代码的行为的关键是理解substring()方法的行为:

From the Javadocs: 从Javadocs:

String java.lang.String.substring(int beginIndex, int endIndex) 字符串java.lang.String.substring(int beginIndex,int endIndex)

Returns a new string that is a substring of this string. 返回一个新字符串,该字符串是该字符串的子字符串。 The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. 子字符串从指定的beginIndex开始,并扩展到索引endIndex-1处的字符。因此,子字符串的长度为endIndex-beginIndex。

Examples: 例子:

"hamburger".substring(4, 8) returns "urge" (not "urger") “ hamburger” .substring(4,8)返回“ urge”(不是“ urger”)

"smiles".substring(1, 5) returns "mile" (not "miles") “ smiles” .substring(1,5)返回“ mile”(不是“ miles”)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM