Java Regular Expression: removes spaces (including width and width) on both sides of the string. spaces inside the string cannot be removed]

Source: Internet
Author: User
Tags string back

Remove spaces (including width and width) on both sides of the string. spaces inside the string cannot be removed.

 

I have tried to use only Java instead of Java regular expressions. lang. although the APIS provided in string can be implemented, you need to write a bunch of code. Now you will take a note of the code that does not use Java regular expressions or Java regular expressions.

 

A does not use Java regular expressions. It is a little troublesome. ^_^

Public class spacechecker {</P> <p> Public static void main (string [] ARGs) {<br/> string STR = "a B C "; </P> <p> system. out. println (delspace (STR); <br/>}</P> <p> Public static string delspace (string Str) {</P> <p> If (STR = NULL) {<br/> return NULL; <br/>}</P> <p> // Delete the halfwidth space first <br/> STR = Str. trim (); </P> <p> while (Str. startswith ("") {</P> <p> // unfortunately, the string does not provide replacelast (), otherwise, it will be simpler. <br/> // after this loop is completed, you can only Spaces are deleted, but backend spaces cannot be deleted. <br/> // after this loop is completed, the string is flipped and then spaces are removed. <br/> STR = Str. replacefirst ("", ""); </P> <p> // trim () is required. Otherwise, if the front-end space is full-width and half-width, it cannot be fixed. <br/> STR = Str. trim (); <br/>}</P> <p> // flip the string <br/> STR = reverse (STR ); </P> <p> // enter a space again <br/> while (Str. startswith ("") {</P> <p> STR = Str. replacefirst ("", ""); </P> <p> STR = Str. trim (); <br/>}</P> <p> // Finally, flip the string back. <br/> return STR = reverse (STR ). trim (); <br/>}</P> <P> // custom string flip method. <Br/> // many third-party packages are implemented, but Java APIs are not implemented. Implement ^ _ ^ here. <br/> Public static string reverse (string Str) {<br/> char [] charsold = Str. tochararray (); </P> <p> char [] charsnew = new char [charsold. length]; </P> <p> int Index = charsold. length-1; </P> <p> for (INT I = 0; I <charsold. length; I ++) {</P> <p> charsnew [I] = charsold [index-I]; <br/>}</P> <p> return string. valueof (charsnew); <br/>}< br/>}

B. Use a Java regular expression.

Public class spacechecker {</P> <p> Public static void main (string [] ARGs) throws exception {<br/> spacecheck SC = new spacecheck (); </P> <p> string STR = "a B C"; </P> <p> system. out. println (SC. delspace (STR); <br/>}</P> <p> Public String delspace (string Str) throws exception {</P> <p> If (STR = NULL) {<br/> return NULL; <br/>}</P> <p> string regstartspace = "^ [] *"; <br/> string regendspace = "[] * $ "; </P> <p> // two replaceall in a row <br/> // The first one is to remove the leading space, the second one is to remove the backend space <br/> // originally intended to be in one breath, but it cannot be implemented ^ _ ^ <br/> string strdelspace = Str. replaceall (regstartspace ,""). replaceall (regendspace, ""); </P> <p> return strdelspace; <br/>}< br/>}

 

Take a note and remember to study Java regular expressions.

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.