Got String
which contains two symbols:
String s = "AB";
How to add space " "
between?
Got String
which contains two symbols:
String s = "AB";
How to add space " "
between?
A String is immutable which means you can't change it, so you need to create a new String.
One way to do this is to use a StringBuilder
:
StringBuilder
using the original stringStringBuilder.insert(...)
method to insert a charactertoString()
method of the StringBuilder
.A simple way can be to split the string on each character and join the parts using space as the delimiter.
Demo:
public class Main {
public static void main(String[] args) {
String s = "AB";
s = String.join(" ", s.split(""));
System.out.println(s);
}
}
Output:
A B