The meaning and usage scenarios of the .trim() method

learning target:

学习目标如下所示:

  • The meaning and usage of .trim() method

Learning Content:

内容:

  1. Meaning:
    The trim() function removes whitespace characters or other predefined characters on both sides of a string.
    Function removes spaces or other characters from the beginning and end of a string. When the function executes successfully, it returns a string with leading and trailing spaces removed from the string string.
    If an error occurs, it returns an empty string (). If the value of any parameter is NULL, the Trim() function returns NULL.
  2. In Java, using the Trim function requires calling the trim() method of the String class.
  3. This method will return a new string and remove invisible characters such as spaces, carriage returns, and line feeds at the beginning and end of the original string.
  4. Calling method: string.trim()
  5. trim is a method that removes spaces from a string and returns a new string.
  6. trim does not need to pass parameters, it directly processes the current calling object. Secondly, it needs a return value. This in the function refers to the calling object, so there is no need to pass parameters.

Knowledge summary:

小结:

  • 1. The TRIM function in SQL is used to remove the beginning or end of a character in a string. The most common use is to remove leading or trailing whitespace.

  • 2. Related function
    ltrim() - removes blank characters or other predefined characters on the left side of the string.
    rtrim() - Removes whitespace characters or other predefined characters from the right side of a string.

  • 3. Trim() can not only remove spaces, but also remove some other redundant symbols.
    These symbols are:

    \t  \n  \v  \f  \r  \x0085  \x00a0  ?  \u2028  \u2029
    

    They are: horizontal tab character, line feed character, vertical tab character, page feed character, and carriage return. Except for the question mark, the rest are written in the form of escape characters.

  • 4. The trim() function removes blank characters or other predefined characters on both sides of the string.

Function removes spaces or other characters from the beginning and end of a string. When the function executes successfully, it returns a string with leading and trailing spaces removed from the string string. When an error occurs, it returns an empty string (""). If the value of any parameter is NULL, the Trim() function returns NULL.

  • 5. Find the first character that is not a space and the last character that is not a space, and intercept the content in between. You
    can also use substring to achieve this, as shown below:
    let startIndex = Math.max(this.search(/\S/), 0 );
    let endIndex = this.search(/\S\s*$/) + 1;
    return this.substring(startIndex, endIndex);
  • 6. It can also be implemented using regular expressions,
    \s is used to match spaces, ^ is used to match the beginning of a space, the space can be 1 or more, * means to match one or more
  • [x] 7. Use for loop: replace the original string every time a space is found.
    When complex strings need to be processed, we try not to use for loop.

Guess you like

Origin blog.csdn.net/YHLSunshine/article/details/132028010