Correct way to trim a string in Java

后端 未结 8 886
独厮守ぢ
独厮守ぢ 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:51

    As strings in Java are immutable objects, there is no way to execute trimming in-place. The only thing you can do to trim the string is create new trimmed version of your string and return it (and this is what the trim() method does).

提交回复
热议问题