Removing whitespace from strings in Java

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

    Using Pattern And Matcher it is more Dynamic.

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class RemovingSpace {
    
        /**
         * @param args
         * Removing Space Using Matcher
         */
        public static void main(String[] args) {
            String str= "jld fdkjg jfdg ";
            String pattern="[\\s]";
            String replace="";
    
            Pattern p= Pattern.compile(pattern);
            Matcher m=p.matcher(str);
    
            str=m.replaceAll(replace);
            System.out.println(str);    
        }
    }
    

提交回复
热议问题