Introduction of Trim of TrimStart of TrimEnd of

  • 2020-06-07 05:13:45
  • OfStack

C# Trim(), TrimStart(), TrimEnd()
These three methods are used to remove certain characters that appear at the beginning and end of a string. Trim() removes Spaces at the beginning and end of the string from outside to inside until it hits a non-space character, so no matter how many consecutive Spaces before and after are removed. TrimStart() removes only the Spaces at the head of the string. TrimEnd() removes only the Spaces at the end of the string.
If these three functions take an argument to a char array, then any character that appears in the char array is deleted. For example, Trim("abcd".ToCharArray ()) is the deletion of a or b or c or d characters that appear at the beginning and end of the string. The deletion process does not end until one character is encountered that is neither a nor b nor c nor d.
The most common misconception here is that the "abcd" string was deleted. The following cases:
string s = " from dual union all ";
s = s.Trim().TrimEnd("union all".ToCharArray());
One might think that the final result of s above is "from dual", but the real result is "from d". It should be noted that the deletion object performed in this way is any character in the character array, not a string consisting of these characters hyphenated from 1!

1 general TRIM function usage:
Trim() removes Spaces at the beginning and end of a string.
Grammar Trim (string)
Parameter string: string, specifying the string return value String to remove header and tail Spaces.
The function returns a string that removes Spaces at the beginning and end of the string string on success, and an empty string ("") on error.
If the value of any argument is NULL, the Trim() function returns NULL.
The TRIM function in SQL is used to remove the head or tail of a string.
The most common use is to remove white space at the beginning or end of a word.
This function has different names in different databases :MySQL: TRIM(), RTRIM(), LTRIM() Oracle: RTRIM(), LTRIM() SQL Server: RTRIM(), LTRIM()
The syntax for the various trim functions is as follows:
TRIM([[position] [string to be removed] FROM] string): The possible values for [position] are LEADING (start), TRAILING (end), or BOTH (start and end).
This function removes the string from the beginning, end, or beginning and end of the string. If we don't list what [the string to remove] is, the white space will be removed.
LTRIM(string): Removes all whitespace at the beginning of a string. RTRIM(string): Removes white space at the end of all strings.

Related articles: