String.Trim () source Analysis __java

Source: Internet
Author: User
Public String trim () {
    int len = value.length;
    int st = 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;
}
St=0, start the loop from the left of the array, and when you encounter the first character that is not empty, jump out of the loop

Then start the loop from the right of the array, and jump out of the loop when you encounter the first character that is not empty.

Finally, the array is returned.

 
String str1 = "   333";
String str2 = "All   ";
String STR3 = "   444  ";
String STR4 = "555   666";
System. out. println (Str1.trim ());
System. out. println (Str2.trim () + "removed");
System. out. println ("removed" +str3.trim () + "removed");
System. out. println (Str4.trim ());

Output: 333
Did 22 get rid of it?
Did you get rid of the 444?
555 666

Trim () will remove the space between the left and right sides, the middle of the space can not be removed.

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.