Home > Net >  How do I split a String into different Strings without a divider
How do I split a String into different Strings without a divider

Time:05-25

I have a string containing 1 word and I have to split this word into separate characters. I saw some posts about splitting strings but I only found some that split between like - or \\n.

For example

I have the Static string that's "fantastic"
how do I split this into all separate character strings? I do not know what is in the String each time I just have to separate it.

CodePudding user response:

You could iterate like this:

String s = "ABCDE";
s.chars().forEach(c -> System.out.println((char) c));

Or you could collect them in a List by using this instead (Requires Java version 16):

List<String> list = s.chars().mapToObj(c -> String.valueOf((char) c)).toList();

or if you prefer an array:

EDIT: simplified

String[] array = s.split("");
  • Related