Detailed description of the trim() method in java.lang.String

What exactly does the String.Trim() method do for us, is it just removing spaces from both ends of the string?

I always thought that the Trim() method is to delete the space characters at both ends of the string. In fact, I was wrong, and it was ridiculous.

First, I directly decompile the String class and find the Trim() method:

public string Trim()
{
    return this.TrimHelper(WhitespaceChars, 2);
}

The TrimHelper method has two parameters. The first parameter is named WhitespaceChars. The first letter is all capitalized. There must be articles, which is not what I expected:

internal static readonly char[] WhitespaceChars;

 It is just defined here, there is no assignment, and it is static. Let's look at the constructor and find it:

static String()

{ Empty = " "; WhitespaceChars = new char[] { '/t', '/n', '/v', '/f', '/r', ' ', '/x0085', '/x00a0', '?', ' ', ' ', ' ', ' ', '?', '?', '?', '?', '?', ' ', '?', '?', '/u2028', '/u2029', ' ', '?' }; }  

Trim method is to delete these characters at both ends of the string? I have a firm guess.

Continue our exploration, decompile TrimHelper directly, wow, maybe this is what I want, private TrimHelper method:

private string TrimHelper(char[] trimChars, int trimType)
{
    int num = this.Length - 1;
    int startIndex = 0;
    if (trimType != 1)
    {
        startIndex = 0;
        while (startIndex < this.Length)
        {
            int index = 0;
            char ch = this[startIndex];
            index = 0;
            while (index < trimChars.Length)
            {
                if (trimChars[index] == ch)
                {
                    break;
                }
                index++;
            }
            if (index == trimChars.Length)
            {
                break;
            }
            startIndex++;
        }
    }
    if (trimType != 0)
    {
        num = this.Length - 1;
        while (num >= startIndex)
        {
            int num4 = 0;
            char ch2 = this[num];
            num4 = 0;
            while (num4 < trimChars.Length)
            {
                if (trimChars[num4] == ch2)
                {
                    break;
                }
                num4++;
            }
            if (num4 == trimChars.Length)
            {
                break;
            }
            on one--;
        }
    }
    int length = (num - startIndex) + 1;
    if (length == this.Length)
    {
        return this;
    }
    if (length == 0)
    {
        return Empty;
    }
    return this.InternalSubString(startIndex, length, false);
}

 After analysis and operation, I basically know what this method does.

The TrimHelper method has two parameters:

The first parameter trimChars is an array of characters to be removed from both ends of the string;

The second parameter trimType is the type that identifies Trim. As far as it is found, there are 3 values ​​of trimType. When 0 is passed in, the blank characters at the head of the string are removed, when 1 is passed in, the blank characters at the end of the string are removed, and other values ​​(such as 2 ) are passed in to remove the blank characters at both ends of the string.

Finally, let's take a look at the method that actually performs string interception:

private unsafe string InternalSubString(int startIndex, int length, bool fAlwaysCopy)
{
    if (((startIndex == 0) && (length == this.Length)) && !fAlwaysCopy)
    {
        return this;
    }
    string str = FastAllocateString(length);
    fixed (char* chRef = &str.m_firstChar)
    {
        fixed (char* chRef2 = &this.m_firstChar)
        {
            wstrcpy(chRef, chRef2 + startIndex, length);
        }
    }
    return str;
}

 Did you use pointers? The first time I saw it, the efficiency should be higher. 
Finally, to sum up: 
The String.Trim() method will remove both ends of the string, not only space characters , it can remove a total of 25 characters: 
('/t', '/n', '/v', '/f ', '/r', ' ', '/x0085', '/x00a0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '?', '/u2028', '/u2029', ' ', '?')


If you want to keep one or more of them (eg /t tab, /n newline, /r carriage return, etc.), use the Trim method with caution.

Please note that the process of Trim deletion is from the outside to the inside until a non-blank character is encountered, so no matter how many consecutive blank characters there are before and after, it will be deleted.

 Finally, two related methods (also directly provided by the String class) are attached, the TrimStart method for removing blank characters at the head of the string and the TrimEnd method for removing blank characters at the end of the string:

TrimStart and TrimEnd methods

If you want to remove any other characters at both ends of the string, you can consider Trim's overloaded brother: String.Trim(Char[]), passing in an array of which characters you want to remove.

The source code is provided:

public string Trim(params char[] trimChars)
{
    if ((trimChars == null) || (trimChars.Length == 0))
    {
        trimChars = WhitespaceChars;
    }
    return this.TrimHelper(trimChars, 2);
}

Space != whitespace, to remove spaces please use: Trim(' ');

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326119211&siteId=291194637