I\'m writing following program to separate characters from a string and assign it to an array.
public class Str {
public static void main(String[] args)
Like others have noted, using toCharArray()
is the preferred method to do this kind of operation. Since the OP wants to know why the first string is empty when using split("")
, it is because empty string is the first match even before the 1st character is parsed. As per documentation, only trailing empty strings are ignored and not the leading empty strings.
In order to work around this problem you can use negative look behind. So, to use split and ignore the first empty string your regex should be:
str.split("(?