Removing whitespace from strings in Java

前端 未结 30 2396
一个人的身影
一个人的身影 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:26

    replaceAll("\\s","")
    

    \w = Anything that is a word character

    \W = Anything that isn't a word character (including punctuation etc)

    \s = Anything that is a space character (including space, tab characters etc)

    \S = Anything that isn't a space character (including both letters and numbers, as well as punctuation etc)

    (Edit: As pointed out, you need to escape the backslash if you want \s to reach the regex engine, resulting in \\s.)

提交回复
热议问题