About the Trim () method in the String class in Java

The

Trim method removes the white space characters at both ends of the string. There are some questions about the return value. Why do you have to judge the subscript? I understand that no matter whether the string has blank characters or not, you can return the substring.

public String trim() {
        int len = value.length;
        int st = 0;
        char[] val = value;    /* avoid getfield opcode */

        while ((st < len) && (val[st] <= " ")) {
            stPP;
        }
        while ((st < len) && (val[len - 1] <= " ")) {
            len--;
        }
        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
    }

finally, return is not very understanding, ask for help, thank you ~!

Mar.23,2021

if there are white space characters, return the new string after substring, otherwise return itself.


st indicates the start position of no spaces, len indicates the end position of no spaces
st > 0 indicates leading spaces, and len < length indicates post spaces
so make OR judgment: if there are spaces, return a substring,
if there are no spaces before and after, return this


substring method will create a new String object. The subscript is judged here to minimize the number of objects.


if the string does not have a white space character, it directly returns this (itself). If the white space character, use subString () to create a new String return.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e609a0-458c3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e609a0-458c3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?