The. Trim () method of String in Java

Source: Internet
Author: User

This method removes whitespace characters from both sides

Principle:

See the source code implementation

 PublicString Trim () {intLen =value.length; intSt =0; Char[] val = value;/*avoid GetField opcode*/         while(St < Len) && (Val[st] <=' ') ) {St++; }         while(St < Len) && (Val[len-1] <=' ') ) {len--; }        return(St >0) || (Len < value.length)) ? SUBSTRING (ST, Len): This; }
View Code

According to the source, the string is split into a character array, and then the ASCII code of the characters from both ends of the character array is compared to the ASCII code of the space character, then the first start and the last (inclusive) characters greater than 32 are taken.

Attached: integer.valueof ("); The ASCII code that can view the space is ASCII code of 32,TAB is 9.

Extension: the definition of whitespace in Java

The. Trim () method of String in Java

Related Article

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.