How to Remove string spaces in JAVA

Source: Internet
Author: User

Delete spaces in strings in js. Common functions include String. trim removes spaces at the beginning and end, but removes all spaces at the beginning and end, including regular str. replace to replace Spaces

1. String. trim ()

Trim () is to remove spaces at the beginning and end

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

The Code is as follows: Copy code


String str = "hell o ";
String str2 = str. replaceAll ("","");
System. out. println (str2 );

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

The Code is as follows: Copy code

4. str =. replaceAll ("\ s *","");

Can replace most of the white space characters, not limited to spaces

S can match any of the spaces, tabs, page breaks, and other blank characters.


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

The Code is as follows: Copy code

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 ();
}

Let's take a look at the instance.

 

The Code is as follows: Copy code
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 method: String s = "the String you want to remove ";
. Remove spaces: s = s. replace ('\ s ','');
. Remove the carriage return: s = s. replace ('n ','');
In this way, the space and press ENTER can be removed, and others can be taken as well.
Note: n press enter (u000a)
T horizontal tab (u0009)
S space (u0008)
R line feed (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.