I want to split a string like "My dog" into an array of:
| M | y | space char will be in here | D | o | g |
Here is my code:
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]);
}
EDIT:
It only prints out "My"
java.lang.String has a toCharArray() that does exactly that.
If all you see if "My", that is all you have in your input
string. Did you use Scanner.next() ?
for(String s : "My dog".split(""))
System.out.println(s);
prints
{empty line}
M
y
d
o
g
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With