How to remove multiple spaces from string in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = " java   php    python       c           c++         c#    ";
        
        s = (s.replaceAll(" +", " ")).trim(); 
        
        System.out.println(s);
    }
}



/*
run:

java php python c c++ c#

*/

 



answered Dec 15, 2020 by avibootz
...