Take five minutes to find out what String.trim() did

Where String.trim() is used in the project, click to see trim(). Previously, I thought that trim() was only used to remove the "space" character at the beginning and end of the string, but from the source code, it seems that this statement is not accurate.
In terms of technology, be more rigorous. Record it here, and the big guys will have a look~

Talk about our cognitive trim()

When it comes to the String.trim() function, as the foreword says, I think it is used for leading and trailing spaces, although this statement is also correct.

That's all said. It's unreasonable not to try, isn't it:

String str = " Hello World ";
System.out.println(str.length());
System.out.println(str.trim().length());

The small characters are narrators: "did you get rid of it? It looks like you got rid of it." so it's OK to remove the spaces at the beginning and end.

Review a small concept

The Java language specification stipulates that the char type of Java is the code unit of UTF-16, that is, it must be 16 bits (2 bytes).
The range of char (character) is: 0-65535 or(\u0000~\uFFFF)
Look at the picture!

I know a big man's answer: How many bytes does a character occupy in the Java language?
How can I let you guys see other links? Of course, if I have time, I will sort out a single one and offer it.

Form a good habit

Develop a good habit and look at the source code. In fact, many times, the comments in the source code or the source code process have been well explained.
And we are often reluctant to see the source code.
Small characters are narrators: "the source code is all in English, and I have a headache when I look at it. After reading it, I can solve the migraine ~"

Of course, even if you really don't want to see the source code. Boss, you have the honor to come in. Naturally, I'll tell you.

In fact, the source code comments of trim() in JDK 1.8 have been clearly explained:

1. Delete any leading and trailing spaces;
2. If a String object represents a null character, a reference to this String object is returned;
Summary: [the leader of trim said: you have a twenty-five cub (space), I'll change a new cub for you]
3. If the encoding of the first and last characters of the character represented by the String object is greater than '\ u0020' (space character), return a reference to the String object;
Summary: [the leader of trim said: there are brothers before and after the space, I won't fuck you]
4. If there is no character greater than '\ u0020' in the string, a string object representing an empty string is returned;
Summary: [the leader of trim() said: look at your pity, I'll give you a new baby (new String object, empty, length 0)]
5. Suppose K is the index of the first character in the string with code greater than '\ u0020', and suppose m is the index of the last character in the string with code greater than '\ u0020'. Returns a substring representing the string. The substring starts with the character at the subscript k and ends with the character at the subscript M. substring(k, m + 1).
Summary: [the leader of trim said: I'll take all the 25 cubs (less than '\ u0020') before and after. Honest cubs and 25 cubs with brothers have left you]
6. If the length of the String after using trim() is not equal to the length before using, a new String object will be returned to you; If equal to, return it to your previous cub (the original String object).
Conclusion: [the leader of trim said: it doesn't mean - > your cub is greasy (the len gt h has changed), so I'll give you a new cub; it means - > your cub is OK, take it back and raise it well]

Move, don't talk nonsense

Examples 1-6 correspond to the above seven trim() source code descriptions. Practice is the only standard to realize truth. Practice is realization. I opened it directly, guys.

Example 1

   /**
	* 1.Delete any leading and trailing spaces;
	*/
	@Test
	public void str1(){
	   String str = " Hello World ";
	   System.out.println(str.length());
	   System.out.println("trim(): " + str.trim().length());
	}

Example 2

   /**
     * You have a 25 cub here (space), I'll change a new cub for you
     */
    @Test
    public void str2(){
        // If a String object represents a null character, a reference to the String object is returned;
        String str1 = " ";
        System.out.println(str1.length());
        String str2 = str1.trim();
        System.out.println("trim()->" + str2.length());
        System.out.println(str1 == str2);
    }

Example 3

    /**
     * There are brothers before and after the space. I won't fuck you
     */
    @Test
    public void str3(){
        String str1 = "aaa bbb ccc";
        System.out.println(str1.length());
        String str2 = str1.trim();
        System.out.println(str2.length());
    }

Example 4

  /**
    * Look at your pity. I'll give you a new baby (new String object, empty, length 0)
    */
    @Test
    public void str4(){
        // You have 32 cubs;
        char[] chars = new char[32];
        for (int i = 0; i < 32; i++) {
            chars[i] = (char) i;
        }
        String oldStr = new String(chars);
        System.out.println(oldStr.length());
        // Unfortunately, there are 32 twenty-five cubs
        String newStr = oldStr.trim();
        System.out.println(newStr.trim().length());
        System.out.println(oldStr == newStr);
    }

Example 5

   /**
     * Suppose k is the index of the first character in the string whose code is greater than '\ u0020',
     * Suppose m is the index of the last character in a string whose code is greater than '\ u0020'.
     * Returns a substring representing the string, which is marked at k
     * The character begins with the character and ends with the character at the following mark m. substring(k, m + 1).
     */
    @Test
    public void str5(){
        // Prepare eight cubs first
        char[] chars = new char[8];
        // Three twenty-five cubs
        for (int i = 0; i < 3; i++) {
            chars[i] = (char) i;
        }
        chars[3] = 65;
        chars[4] = 66;
        // Twenty five cubs (less than the space character, the space character is 32)
        chars[5] = 31;
        chars[6] = 68;
        // Twenty five cubs (less than the space character, the space character is 32)
        chars[7] = 21;
        String oldStr = new String(chars);
        System.out.println("oldStr.length(): " + oldStr.length());
        System.out.println("oldStr: " + oldStr);
        String newStr = oldStr.trim();
        System.out.println("newStr.length(): " + newStr.length());
        System.out.println("newStr: " + newStr);
    }

Example 6

/**
     * It doesn't mean - > your cub is fishy (the len gt h has changed), so I'll replace you with a new cub; No problem with your baby. Take it back and raise it well
     */
    @Test
    public void str6(){
        // Prepare five cubs
        char[] chars = new char[5];
        // Three twenty-five cubs
        for (int i = 0; i < 3; i++) {
            chars[i] = (char) i;
        }
        chars[3] = 65;
        chars[4] = 66;
        String str1 = new String(chars);
        String str2 = "ABCDE";

        String newStr1 = str1.trim();
        String newStr2 = str2.trim();


        System.out.println("str1.length(): " + str1.length());
        System.out.println("newStr1.length(): " + newStr1.length());
        System.out.println(str1 == newStr1);
        System.out.println("=========================\n");

        System.out.println("str2.length(): " + str2.length());
        System.out.println("newStr2.length(): " + newStr2.length());
        System.out.println(str2 == newStr2);
    }

Look directly here in five minutes

What exactly did trim() do

  1. Remove the leading and trailing spaces in the String;
  2. Cannot remove the string containing spaces;
  3. To be exact, all characters less than decimal 32 (32 is a space, you can see the Ascii table) are actually removed.
  4. When the String is full of characters less than 32, a new character is returned to you.
  5. String.trim() the length change is a new object. If there is no change, it is still itself.
Thank you for reading and praising, with a daily salary of more than 10000 ~!!!

Tags: Java string

Posted on Sun, 28 Nov 2021 03:25:31 -0500 by nashsaint