Removing whitespace from strings in Java

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

    In java we can do following operation:

    String pattern="[\\s]";
    String replace="";
    part="name=john age=13 year=2001";
    Pattern p=Pattern.compile(pattern);
    Matcher m=p.matcher(part);
    part=m.replaceAll(replace);
    System.out.println(part);
    

    for this you need to import following packages to your program:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    

    i hope it will help you.

提交回复
热议问题