javastringremoving-whitespace

leading whitespace while using string.split()


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.


Solution

  • Try this:

    char[] str = "1234".toCharArray();
    System.out.println(str.length) ;