Correct way to trim a string in Java

后端 未结 8 890
独厮守ぢ
独厮守ぢ 2020-12-15 04:30

In Java, I am doing this to trim a string:

String input = \" some Thing \";
System.out.println(\"before->>\"+input+\"<<-\");
input = input.trim(         


        
8条回答
  •  庸人自扰
    2020-12-15 04:42

    The java string trim() method eliminates leading and trailing spaces

    public class StringTrimExample{  
    public static void main(String args[]){  
    String s1="  hello string   ";  
    System.out.println(s1+"javatpoint");//without trim()  
    System.out.println(s1.trim()+"javatpoint");//with trim()  
    }}  
    

    output

     hello string   javatpoint
    hello stringjavatpoint   
    

提交回复
热议问题