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

后端 未结 4 1438
执笔经年
执笔经年 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:18

    Consider the split expression ",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.

    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.

    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.


    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....).

    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 -1 you will get an extra space at the end)

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

    String[] chars = str.split("(?!(^|$))");
    
    0 讨论(0)
  • 2021-01-11 17:21

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

        // 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

    0 讨论(0)
  • 2021-01-11 17:22

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

    0 讨论(0)
  • 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();

    0 讨论(0)
提交回复
热议问题