Java removes a small example of spaces, carriage returns, line breaks, and tabs in a string _java

Source: Internet
Author: User

Copy Code code as follows:

Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;

/**
* @author Lei
* 2011-9-2
*/
public class StringUtils {

    public static string Replaceblank (String str) {
        String dest = "";
        if (str!=null) {
             pattern p = pattern.compile ("\\s*|\t|\r|\n");
            Matcher m = p.matcher (str);
            dest = M.replaceall ("");
       }
        return dest;
   }
    public static void Main (string[] args) {
        System.out.println (Stringutils.replaceblank ("Just do it!"));
   }
   /*-----------------------------------

Stupid way: string s = "You want to remove the string";
. Remove Space: s = s.replace (' \\s ', ');
. Remove carriage return: s = s.replace (' \ n ', ');

This can also be removed from the space and carriage, others can do so.

Note: \ n carriage return (\u000a)
\ t Horizontal tab (\U0009)
\s Space (\u0008)
\ r line Wrap (\u000d) * *
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.