Home > Java > javaTutorial > Share examples of TrimHelper methods in Java

Share examples of TrimHelper methods in Java

零下一度
Release: 2017-05-20 10:04:15
Original
2132 people have browsed it

The main function of the trim() method we generally use is to remove the leading and trailing spaces of the string. However, according to my personal practical experience, I found that the trim() method can only remove some spaces or whitespace characters, such as half-width spaces; for full-width spaces, trim() cannot remove them. So at this time, you need to use regular rules to remove the leading and trailing spaces, blank characters, newlines or tab characters, newline characters, etc. of the string:

public static void main(String[] args){
     String keyword = "    空格符与制表符等      ";
     keyword = keyword.replaceAll("^[ *| *| *|//s*]*", "").replaceAll("[ *| *| *|//s*]*$", "");
     System.out.println("keyword : "+keyword);      
}
Copy after login

There is also information I found online that explains it this way : First decompile the trim() method and get:

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

After decompiling the TrimHelper method, we get:

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;
            }
            num--;
        }
    }    int length = (num - startIndex) + 1;    if (length == this.Length)
    {        return this;
    }    if (length == 0)
    {        return Empty;
    }    return this.InternalSubString(startIndex, length, false);
}
Copy after login

TrimHelper has two parameters: the first parameter trimChars, is An array of characters to be deleted from both ends of the string; the second parameter trimType is the type that identifies Trim(). There are three values ​​for 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; when other values ​​are passed in, the blank characters at both ends of the string are removed. The final conclusion is: the String.Trim() method will remove both ends of the string, not just space characters, it can remove a total of 25 characters: ('/t', '/n', '/v', ' /f', '/r', ' ', '/x0085', '/x00a0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '?', '/u2028', '/u2029', ' ', '?').

There are two other methods similar to trim(): TrimStart() which removes the blank characters at the beginning of the string and TrimEnd() which removes the blank characters at the end of the string.

If you want to remove any characters at both ends of the string, you can use Trim’s overloaded method: String.Trim(Char[]). The source code of this method is:

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

It should be noted that : Space!= Blank character. If you want to remove spaces, you can use Trim(' ').

【Related recommendations】

1. Detailed explanation of usage examples of java trim

2. Detailed explanation of String.trim in Java () method example

3. trim() in js How to remove spaces on both sides of a string

4. trim() in Java )Function

5. The PHP function trim() that removes spaces and special characters from the beginning and end of a string

The above is the detailed content of Share examples of TrimHelper methods in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template