Removing whitespace from strings in Java

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

    To remove spaces in your example, this is another way to do it:

    String mysz = "name=john age=13 year=2001";
    String[] test = mysz.split(" ");
    mysz = String.join("", mysz);
    

    What this does is it converts it into an array with the spaces being the separators, and then it combines the items in the array together without the spaces.

    It works pretty well and is easy to understand.

提交回复
热议问题