Remove spaces in JAVA

Source: Internet
Author: User

Remove spaces in JAVA:

1. String. trim () is to remove the leading and trailing Spaces

2. String. replace ("", ""); remove all spaces, including the beginning, end, and middle.

. Or String. replaceAll ("+", ""); remove all spaces

4. String. replaceAll ("\ s *", ""); can replace most of the blank characters, not limited to spaces. \ S can match any of the spaces, tabs, page breaks, and other blank characters.

5. Or the following code can remove all spaces, including the beginning, end, and middle.

Public String remove (String resource, char ch ){

StringBuffer buffer = new StringBuffer ();

Int position = 0;

Char currentChar;

While (position <resource. length ()){

CurrentChar = resource. charAt (position ++ );

If (currentChar! = Ch) buffer. append (currentChar );

}

Return buffer. toString ();

}

 

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.