3

here's the code that i am using to split the string

str = "1234".split("") ; 
System.out.println(str.length) ; //this gives 5

there is an extra whitespace added just before 1 i.e str[0]=" " How to split this string without having the leading whitespace.

Pdksock
  • 1,042
  • 2
  • 13
  • 26

6 Answers6

3

Try this:

char[] str = "1234".toCharArray();
System.out.println(str.length) ;
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
  • 3
    Thanks.This does work but why does string.split("") add an extra whitespace at the beginning.There is nothing as such specified in docs. – Pdksock Apr 20 '13 at 05:14
1

You are not actually getting a leading whitespace element, you are getting a leading empty string, as can be seen from printing the whole array:

System.out.println(java.util.Arrays.toString("1234".split("")));

Output:

[, 1, 2, 3, 4]

The rationale for that behavior is the fact that "1234".indexOf("") is 0. I.e., the delimiter string matches at the beginning of the searched string, and thus it creates a split there, giving an empty initial element in the returned array. The delimiter also matches at the end of the string, but you don't get an extra empty element there, because (as the String.split documentation says) "trailing empty strings will be discarded".

However, the behavior was changed for Java 8. Now the documentation also says:

When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.

(Compare String.split documentation for Java 7 and Java 8.)

Thus, on Java 8 and above, the output from the above example is:

[1, 2, 3, 4]

For more information about the change, see: Why in Java 8 split sometimes removes empty strings at start of result array?

Anyway, it is overkill to use String.split in this particular case. The other answers are correct that to get an array of all characters you should use str.toCharArray(), which is faster and more straightforward. If you really need an array of one-character strings rather than an array of characters, then see: Split string into array of character strings.

Boann
  • 48,794
  • 16
  • 117
  • 146
0

Use toCharArray() instead....................

7stud
  • 46,922
  • 14
  • 101
  • 127
  • 1
    Hi and Welcome to Stackoverflow! Please read the [How to Answer a Question Guide](http://stackoverflow.com/questions/how-to-answer). Could you please elaborate you answer a bit more? It's difficult to follow what you're suggesting. Thanks! – slm Apr 20 '13 at 05:28
  • 2
    Ha, no I'm not a VB programmer, I'm a Java, C/C++ Programmer. All that I was saying was that your answer wasn't that complete. Perhaps you could give a more concise example, than just telling him to use the toCharArray() method. Remember that others will come along in the future and see your answer, it's not merely just for the OP. – slm Apr 20 '13 at 06:41
0

if you want to split on "" then should use toCharArray method of string.

String str = "1234";
System.out.println(str.toCharArray().length);
Ahsan
  • 29
  • 1
0

If you are using Java 7 or below, use this:

"1234".split("(?!^)")
Lezorte
  • 473
  • 1
  • 5
  • 13
-1

I would just do:

str = "1 2 3 4".split(" ");  

Unless of course there's a specific reason why it has to be "1234"

Michael Hall
  • 187
  • 5