How to remove multiple spaces inside a String and just leave single space.
Eg: Input: "java on java"Output: "java on java"
Answer:
In Java, there is function replaceAll, we can simply use that as below, In C this was a bit cumbersome to do, but as they say, Java is Java. We can do it easy here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String stringWithMultipleSpaces = "java on java"; | |
System.out.println("Original String: " + stringWithMultipleSpaces); | |
System.out.println("Output String: " = stringWithMultipleSpaces.replaceAll("\\s+"," ")); | |
//Original String: java on java | |
//Output String: java on java |