Trim is easy to use.Write about how the.NET class library is implemented today.This article starts on the first page and lets you check it out so that you don't keep on reading it incorrectly.A master doesn't laugh.
Trim() function: Removes all matches for white space characters from the start and end of this instance.
Note:
(1)Trim Method Removes all leading and trailing whitespace characters from the current string.Each leading and tail clipping operation stops when a non-whitespace character is encountered.
(2) This method does not modify the value of the current instance.Instead, it returns a new string in which all leading and trailing white space characters found in the current instance are removed.
(3) If the current string equals Empty Or all characters in the current instance consist of whitespace characters, which the method returns Empty.
Function of Trim(char[] trimChars): Removes all matches for a set of characters specified in the array from the beginning and end of this instance.
Note:
(1) Removing all matches for characters in trimChars from the beginning and end of this instance String .If trimChars is a blank reference (Nothing in Visual Basic), remove the blank character instead.
(2) This method does not modify the value of the current instance.Instead, it returns a new string in which all leading and trailing trimChars found in the current instance are removed.
(3) If the current string equals Empty Or all characters in the current instance consist of characters in the trimChars array, which returns Empty.
Functions of TrimStart(char[] trimChars): From Current String Object removes all leading matches for a set of characters specified in the array.(Attention points are similar to Trim)
Functions of TrimEnd(char[] trimChars): From Current String Object removes all trailing matches for a set of characters specified in the array.(Attention points are similar to Trim)
A more detailed explanation of the Trim method in MSDN is provided.
The code in the.NET 2.0 class library is as follows:
private const int TrimHead = 0; private const int TrimTail = 1; private const int TrimBoth = 2; public static readonly String Empty = "";
internal static readonly char[] WhitespaceChars = { (char) 0x9, (char) 0xA, (char) 0xB, (char) 0xC, (char) 0xD, (char) 0x20, (char) 0x85, (char) 0xA0, (char)0x1680, (char) 0x2000, (char) 0x2001, (char) 0x2002, (char) 0x2003, (char) 0x2004, (char) 0x2005, (char) 0x2006, (char) 0x2007, (char) 0x2008, (char) 0x2009, (char) 0x200A, (char) 0x200B, (char) 0x2028, (char) 0x2029, (char) 0x3000, (char) 0xFEFF };
Trim() method:
public String Trim() { return TrimHelper(WhitespaceChars,TrimBoth); }
The TrimHelper method was called.TrimHelper (Whitespace Chars, 2); removes spaces from the beginning and end.*
Implementation of the TrimHelper method:
public String TrimHelper(char[] trimChars, int trimType) { //end will point to the first non-trimmed character on the right //start will point to the first non-trimmed character on the Left int end = this.Length-1; int start=0; //Trim specified characters. if (trimType !=TrimTail) { for (start=0; start < this.Length; start++) { int i = 0; char ch = this[start]; for( i = 0; i < trimChars.Length; i++) { if( trimChars[i] == ch) break; } if( i == trimChars.Length) { // the character is not white space break; } } } if (trimType !=TrimHead) { for (end= Length -1; end >= start; end--) { int i = 0; char ch = this[end]; for(i = 0; i < trimChars.Length; i++) { if( trimChars[i] == ch) break; } if( i == trimChars.Length) { // the character is not white space break; } } } //Create a new STRINGREF and initialize it from the range determined above. int len = end -start + 1; if (len == this.Length) { // Don't allocate a new string is the trimmed string has not changed. return this; } else { if( len == 0) { return String.Empty; } return InternalSubString(start, len, false); } }
Analyzing the above functions, we know:
Judging from the incoming trimType value is the removal of white space characters at the beginning, end or beginning.
Check the first for loop to see if the first non-empty character encountered before the string is a non-empty character. If it is a non-empty character, jump out of the loop and record the start value.
The second for loop determines that the first non-empty character encountered at the end of the string is a non-empty character, jumps out of the loop if it is a non-empty character, and records the end value.
end survives a new string based on start.
The InternalSubString method is as follows:
unsafe string InternalSubString(int startIndex, int length, bool fAlwaysCopy) { BCLDebug.Assert( startIndex >= 0 && startIndex <= this.Length, "StartIndex is out of range!"); BCLDebug.Assert( length >= 0 && startIndex <= this.Length - length, "length is out of range!"); if( startIndex == 0 && length == this.Length && !fAlwaysCopy) { return this; } String result = FastAllocateString(length); fixed(char* dest = &result.m_firstChar) fixed(char* src = &this.m_firstChar) { wstrcpy(dest, src + startIndex, length); } return result; }
Part of the code is as above.
The Trim(char[] trimChars) code is as follows:
public String Trim(params char[] trimChars) { if (null==trimChars || trimChars.Length == 0) { trimChars=WhitespaceChars; } return TrimHelper(trimChars,TrimBoth); }
The implementation is similar to the Trim() method except that the trimChars character array is replaced with its own defined TrimChars character array.
The TrimStart method is similar to the Trim(char[] trimChars) method.
public String TrimStart(params char[] trimChars) { if (null==trimChars || trimChars.Length == 0) { trimChars=WhitespaceChars; } return TrimHelper(trimChars,TrimHead); }
The TrimEnd method is similar to the Trim(char[] trimChars) method.
public String TrimEnd(params char[] trimChars) { if (null==trimChars || trimChars.Length == 0) { trimChars=WhitespaceChars; } return TrimHelper(trimChars,TrimTail); }
If you have any questions, please pat the bricks in time.
Reproduced at: https://www.cnblogs.com/thinkquan/archive/2012/04/01/trimpractice.html