Looking here ( String (Java Platform SE 7 ) ), I understand that the trim() method of the String class "returns a copy of the string, with leading and trailing whitespace omitted", but I don't understand what the last special case involving Unicode characters is exactly.

 

Looking here ( List of Unicode characters - Wikipedia, the free encyclopedia ), I see that U+0020 is a space character, and I also see the characters that follow the space character (such as the exclamation mark character).

 

So, I decided to write a small code sample to try and replicate the behaviour that I quoted (from the API documentation of the trim method) in the multi-line comment of this same code sample. Here is the code sample.:

public class TrimTester {

    public static void main(String[] args) {
       
        /*
         * "Otherwise, let k be the index of the first character in the string whose code
         * is greater than '\u0020', and let m be the index of the last character in the
         * string whose code is greater than '\u0020'. A new String object is created,
         * representing the substring of this string that begins with the character at
         * index k and ends with the character at index m-that is, the result of
         * this.substring(k, m+1)."
         */
        String str = "aa!Hello$bb";
        System.out.println(str.trim());
    }
}

 

However, what is printed is "aa!Hello$bb" (without the quotes) instead of "!Hello$" (without the quotes).

 

Any input to help me better understand what is going on would be greatly appreciated!

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners