java - 拆分字符串后,数组中的第一个元素是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19993386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:08:05  来源:igfitidea点击:

java - after splitting a string, what is the first element in the array?

javaregexarraysstring

提问by user2994814

I was trying to split a string into an array of single letters. Here's what I did,

我试图将一个字符串拆分为一组单个字母。这是我所做的,

String str = "abcddadfad"; 
System.out.println(str.length());    //  output: 10  
String[] strArr = str.split("");  
System.out.println(strArr.length);   //  output: 11   
System.out.println(strArr[0]);       // output is nothing 

The new array did contain all the letters, however it has nothing at index 0,not even a white space, but still incremented the size of my array. Can anyone explain why this is happening?

新数组确实包含所有字母,但是它在索引 0 处没有任何内容,甚至没有空格,但仍然增加了我的数组的大小。谁能解释为什么会这样?

回答by Jesse Nelson

You can just use the built in java method from the string class. myString.toCharArray()the empty string is being stored at index 0

您可以只使用 string 类中的内置 java 方法。myString.toCharArray()空字符串存储在索引 0

回答by rolfl

Consider the split expression ",1,2,3,4".split(",");

考虑拆分表达式 ",1,2,3,4".split(",");

What would you expect? Right, an empty-string to start with. In your case you have a 'nothing' in front of the first 'a' as well as one behind it.

你会期待什么?是的,从一个空字符串开始。在您的情况下,您在第一个 'a' 前面和后面都有一个 'nothing'。

Update: comments indicate this explanation is not enough of an explanation (which it may not be)... but, it really is this simple: the engine starts at the beginning of the string, and it looks to see if what's in front of it matches the pattern. If it does, it assigns what's behind it to a new item in the split.

更新:评论表明这个解释还不够解释(可能不是)......但是,它真的很简单:引擎从字符串的开头开始,它会查看前面是否有什么它匹配模式。如果是,它会将其后面的内容分配给拆分中的新项目。

On the first character, it has "" (nothing behind it), and it looks to see if there's "" (the pattern) in front of it. There is, so it creates a "" match.

在第一个字符上,它有“”(后面没有任何东西),它会查看前面是否有“”(模式)。有,所以它创建了一个 "" 匹配。

It then moves on, and it has 'a' behind it, and again, it again has "" in front of it. So the second result is an "a" string.

然后它继续前进,它的后面有一个“a”,它的前面又是一个“”。所以第二个结果是一个“a”字符串。

An interesting observation is that, if you use split("", -1)you will also get an empty-string result in the last position of the result array.

一个有趣的观察是,如果您使用,split("", -1)您还将在结果数组的最后一个位置得到一个空字符串结果。



Edit 2: If I wrack my brains further, and consider this to be an academic exercise (I would not recommend this in real life...) I can think of only one good way to do a regex split()of a String into a String[]array with 1 character in each string (as opposed to char[] - which other people have given great answers for....).

编辑 2:如果我进一步绞尽脑汁,并认为这是一个学术练习(我不会在现实生活中推荐这个......)我只能想到一种split()将字符串的正则表达式转换为String[]数组的好方法每个字符串中的 1 个字符(与 char[] 相反 - 其他人已经给出了很好的答案......)。

String[] chars = str.split("(?<=.)", str.length());

This will look behind each character, in a non-capturing group, and split on that, and then limit the array size to the number of characters (you can leave the str.length()out, but if you put -1you will get an extra space at the end)

这将查看每个字符的后面,在一个非捕获组中,并对其进行拆分,然后将数组大小限制为字符数(您可以str.length()省略,但如果您放入,-1您将在最后获得一个额外的空间)

Borrowing nitro2k01's alternative (below in the comments) which references the string beginning and end, you can split reliably on:

借用 nitro2k01 的替代方案(在评论下方),它引用了字符串的开头和结尾,您可以可靠地拆分:

String[] chars = str.split("(?!(^|$))");

回答by mangr3n

I'd have to read the code, to understand exactly how "" works as a regular expression. However, remember it's matching the empty string... and the argument is a regular expression, and the javadoc mentions that calling split(regex) is the same as calling split(regex,0). Therefore it will NOT try to match again if the remaining string is all whitespace (or emptystring), which is why it doesn't match the final emptystring after the last character.

我必须阅读代码,才能确切了解 "" 作为正则表达式的工作方式。但是,请记住它匹配空字符串......并且参数是一个正则表达式,javadoc 提到调用 split(regex) 与调用 split(regex,0) 相同。因此,如果剩余的字符串都是空白(或空字符串),它不会再次尝试匹配,这就是为什么它不匹配最后一个字符之后的最终空字符串。

The better function to call might be str.toCharArray();

更好的调用函数可能是 str.toCharArray();

回答by Tarsem Singh

You can Also try this way

你也可以试试这种方式

String str = "abcddadfad";
System.out.println(str.length());  // output: 10
String[] strArr = new String[str.length()];
for (int i = 0; i < strArr.length; i++) {   

strArr[i] = "" + str.charAt(i);

strArr[i] = "" + str.charAt(i);

    // As per  ratchet freak comment: it's easier (and more efficient) to use 
     strArr[i] = substring(i,i+1);
}
System.out.println(strArr.length); // output: 10
System.out.println(strArr[0]);     // output: a

As per

按照