Teach you to use Java implementation to remove various Spaces

  • 2020-04-01 03:48:14
  • OfStack

1. String. The trim ()

Trim () is to remove head-and-tail Spaces

2. STR. Replace (" ", ""); Remove all Spaces, including head, tail and middle

String STR = "hell o ";
String str2 = STR. ReplaceAll (" ", "");
System. The out. Println (str2);

3. Or replaceAll(" +",""); Get rid of all Spaces

4. The STR =. ReplaceAll (" \ s * ", "");

Can replace most white space characters, not limited to Spaces
S can match any of the whitespace characters such as Spaces, tabs, page breaks, etc

5. Or the following code can also remove all the Spaces, including the beginning, the end, and the 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();
  }

The above is the article to share all the content, I hope you can enjoy.

Please take a moment to share the article with your friends or leave a comment. We sincerely appreciate your support!


Related articles: