拆分没有分隔符的字符串

Ade*_*e A 4 java arrays string split

我想将像"我的狗"这样的字符串分成以下数组:

| M | y | space char will be in here | D | o | g |
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

String []in_array;
    input = sc.next();  
in_array = input.split(""); //Note this there is no delimiter 

for(int k=1; k < in_array.length; k++){
    System.out.print(" "+in_array[k]);
}
Run Code Online (Sandbox Code Playgroud)

编辑:

它只打印出"我的"

Ale*_*øld 9

java.lang.String有一个toCharArray()就是这样做的.

  • -1,它给你一个`char`数组,而不是`String`数组 (4认同)

Pet*_*rey 5

如果您只看到"我的",那就是您在input字符串中所拥有的一切.你使用Scanner.next()吗?

for(String s : "My dog".split(""))
    System.out.println(s);
Run Code Online (Sandbox Code Playgroud)

版画

{empty line}
M
y

d
o
g
Run Code Online (Sandbox Code Playgroud)