How to remove leading and trailing spaces from a string in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String regex = "^\\s+";
          
        String s = "       java php python         "; 
        
        System.out.println(s.length());
 
        s = s.trim();
 
        System.out.println(s.length());
        System.out.println(s);
    }
}
  
 
 
  
/*
run:
 
31
15
java php python
 
*/

 



answered Jun 28, 2019 by avibootz
edited Nov 1, 2020 by avibootz

Related questions

1 answer 168 views
1 answer 157 views
1 answer 146 views
1 answer 181 views
...