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

后端 未结 4 1437
执笔经年
执笔经年 2021-01-11 16:30

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());    //  ou         


        
4条回答
  •  隐瞒了意图╮
    2021-01-11 17:35

    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.

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

提交回复
热议问题