
java program for removing multiple spaces into single space

Hi Friend,
Try the following code:
class RemoveMultipleSpaces
{
public static void main(String[] args)
{
String str = "Hello World How are you?";
String[] vals = str.split(" ");
StringBuilder sb = new StringBuilder();
for(String s : vals) {
if(s.trim().length() > 0){
sb.append(s).append(" ");
}
}
String out = sb.toString().trim();
System.out.println(out);
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.