Removing whitespace from strings in Java

前端 未结 30 2230
一个人的身影
一个人的身影 2020-11-22 05:01

I have a string like this:

mysz = \"name=john age=13 year=2001\";

I want to remove the whitespaces in the string. I tried trim()

30条回答
  •  一个人的身影
    2020-11-22 05:35

    You should use

    s.replaceAll("\\s+", "");
    

    instead of:

    s.replaceAll("\\s", "");
    

    This way, it will work with more than one spaces between each string. The + sign in the above regex means "one or more \s"

    --\s = Anything that is a space character (including space, tab characters etc). Why do we need s+ here?

提交回复
热议问题