Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 混淆String.split的输出_Java_Regex_String_Class_Core - Fatal编程技术网

Java 混淆String.split的输出

Java 混淆String.split的输出,java,regex,string,class,core,Java,Regex,String,Class,Core,我不理解此代码的输出: public class StringDemo{ public static void main(String args[]) { String blank = ""; String comma = ","; System.out.println("Output1: "+blank.split(",").leng

我不理解此代码的输出:

public class StringDemo{              
    public static void main(String args[]) {
        String blank = "";                    
        String comma = ",";                   
        System.out.println("Output1: "+blank.split(",").length);  
        System.out.println("Output2: "+comma.split(",").length);  
    }
}
并得到以下输出:

Output1: 1 
Output2: 0
案例1-此处为
空白。拆分(“,”
将返回
,因为
空白中没有
,所以长度将为
1

案例2-此处为
逗号。拆分(“,”)
将返回空数组,如果要使用长度
1
计算
逗号,则必须使用scape
,否则长度将
0

再次
comma.split(“,”)
split()需要一个
regex
作为参数,它将返回与该
regex
匹配的结果数组

此方法返回的数组包含此函数的每个子字符串 由与给定字符串匹配的另一个子字符串终止的字符串 表达式或以字符串结尾终止

否则

如果表达式与输入的任何部分不匹配,则 结果数组只有一个元素,即这个字符串

围绕给定正则表达式的匹配项拆分字符串

split()方法的工作原理与调用具有给定表达式和零限制参数的双参数split方法类似因此,结果数组中不包括尾随的空字符串。

如果1
blank.split(“,”)与输入的任何部分都不匹配,则生成的数组只有一个元素,即该字符串。

它将返回整个字符串。
因此,长度将为
1

在这种情况下,2
comma.split(“,”)将返回空。

split()

因此,长度是
0

例如()

字符串“boo:and:foo”使用以下表达式生成以下结果:

参数: 正则表达式-定界正则表达式

返回: 通过围绕给定正则表达式的匹配项拆分此字符串而计算的字符串数组

抛出: PatternSyntaxException-如果正则表达式的语法无效

文件:

用于:
System.out.println(“Output1:+blank.split(“,”).length)

此方法返回的数组包含此字符串的每个子字符串,该子字符串由与给定表达式匹配的另一个子字符串终止,或由字符串结尾终止。数组中的子字符串按它们在此字符串中出现的顺序排列如果表达式与输入的任何部分都不匹配,则生成的数组只有一个元素,即该字符串

它只返回整个字符串,这就是它返回1的原因


对于第二种情况,
String.split
将丢弃
,因此结果将为空

String.split silently discards trailing separators
也可以查看我们可以查看
String.split后面的内容。从兔子洞里走下去的方法

public String[] split(CharSequence input, int limit)
被调用

输入
对于输入
此方法称为

String[] parts = split("", 0);
:

这里的
resultSize==0
limit==0
so.

来自
public String[]split(String regex)
方法的String类:

围绕给定正则表达式的匹配项拆分此字符串

该方法的工作原理类似于使用给定表达式和零限制参数调用双参数split方法。因此,结果数组中不包括尾随的空字符串

在第一种情况下,表达式与输入的任何部分都不匹配,因此我们得到了一个只有一个元素的数组——输入


在第二种情况下,表达式匹配输入,split应该返回两个空字符串;但是,根据javadoc,它们被丢弃(因为它们是尾随的且为空)。

split方法的API声明“如果表达式与输入的任何部分不匹配,则结果数组只有一个元素,即该字符串。”

因此,由于字符串blank不包含“,”,因此返回一个包含一个元素的字符串[](即blank本身)

对于字符串逗号,原始字符串的左边是“nothing”,因此返回一个空数组

如果您想处理返回的结果,这似乎是最好的解决方案。g

String[] splits = aString.split(",");
for(String split: splits) {
   // do something
}
来自JDK1.7

 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.count == 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, count));
                    off = count;
                    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, count));

            // 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);
    }
因此,如果没有匹配的元素,该函数将返回一个包含一个元素的数组

对于第二种情况,
comma.split(“,”)

List List=new ArrayList();
//...
int resultSize=list.size();
如果(限制==0)
while(resultSize>0&&list.get(resultSize-1.length()==0)
结果大小--;
字符串[]结果=新字符串[resultSize];
返回列表。子列表(0,resultSize)。toArray(结果);

正如您所注意到的,上一个while循环已经删除了列表末尾的所有空元素,因此resultSize是
0

一切都按照计划进行,但让我们一步一步来做(我希望您有一些时间)

根据
split(String regex)
方法的(and):

该方法的工作原理类似于使用给定表达式和零限制参数调用双参数split方法

所以当你调用

split(String regex)
实际上,您是从
split(String regex,int limit)
方法中获得结果,该方法以以下方式调用:

split(regex, 0)
因此这里
limit
设置为
0

您需要了解有关此参数的一些信息:

  • 如果
    limit
    为正数,则将结果数组的长度限制为指定的正数,因此
    分割(“x”,2)
    将返回一个数组,
    [“a”,“axa”]
    ,而不是
    [“a”,“a”,“a”,“a”,“a”]
  • 如果
    limit
    0
    ,则不限制
    String[] splits = aString.split(",");
    for(String split: splits) {
       // do something
    }
    
     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.count == 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, count));
                        off = count;
                        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, count));
    
                // 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);
        }
    
    // If no match was found, return this
    if (off == 0)
       return new String[] { this };
    
    List<String> list = new ArrayList<>();
    //...
    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);
    
    split(String regex)
    
    split(regex, 0)
    
    "fooXbarX".split("X")
    
    ["foo", "bar", ""]
    
    ["foo", "bar"]
    
    "fooXbarX".split("X",-1)
    
    ",".split(",").length
    
    ",".split(",", 0).length
    
    for (String s: ",".split(",", -1)){
        System.out.println("\""+s+"\"");
    }
    
    ""
    ""
    
    "".split(",").length
    
    2316 public String[] split(String regex, int limit) {
    2317 /* fastpath if the regex is a
    2318 (1)one-char String and this character is not one of the
    2319 RegEx's meta characters ".$|()[{^?*+\\", or
    2320 (2)two-char String and the first char is the backslash and
    2321 the second is not the ascii digit or ascii letter.
    2322 */
    2323 char ch = 0;
    2324 if (((regex.value.length == 1 &&
    2325 ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
    2326 (regex.length() == 2 &&
    2327 regex.charAt(0) == '\\' &&
    2328 (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
    2329 ((ch-'a')|('z'-ch)) < 0 &&
    2330 ((ch-'A')|('Z'-ch)) < 0)) &&
    2331 (ch < Character.MIN_HIGH_SURROGATE ||
    2332 ch > Character.MAX_LOW_SURROGATE))
    2333 {
    2334 int off = 0;
    2335 int next = 0;
    2336 boolean limited = limit > 0;
    2337 ArrayList<String> list = new ArrayList<>();
    2338 while ((next = indexOf(ch, off)) != -1) {
    2339 if (!limited || list.size() < limit - 1) {
    2340 list.add(substring(off, next));
    2341 off = next + 1;
    2342 } else { // last one
    2343 //assert (list.size() == limit - 1);
    2344 list.add(substring(off, value.length));
    2345 off = value.length;
    2346 break;
    2347 }
    2348 }
    2349 // If no match was found, return this
    2350 if (off == 0)
    2351 return new String[]{this};
    2353 // Add remaining segment
    2354 if (!limited || list.size() < limit)
    2355 list.add(substring(off, value.length));
    2357 // Construct result
    2358 int resultSize = list.size();
    2359 if (limit == 0) {
    2360 while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
    2361 resultSize--;
    2362 }
    2363 }
    2364 String[] result = new String[resultSize];
    2365 return list.subList(0, resultSize).toArray(result);
    2366 }
    2367 return Pattern.compile(regex).split(this, limit);
    2368 }

    if (off == 0)
        return new String[]{this};