Split individual characters from a string

前端 未结 3 1651
粉色の甜心
粉色の甜心 2020-12-12 05:06

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)          


        
3条回答
  •  轮回少年
    2020-12-12 05:48

    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("(?

提交回复
热议问题