Difference between String trim() and strip() methods in Java 11

后端 未结 3 973
悲哀的现实
悲哀的现实 2020-12-01 00:01

Among other changes, JDK 11 introduces 6 new methods for java.lang.String class:

  • repeat(int) - Repeats the String as many times as provided by the
3条回答
  •  醉话见心
    2020-12-01 00:30

    Here is a unit-test that illustrates the answer by @MikhailKholodkov, using Java 11.

    (Note that \u2000 is above \u0020 and not considered whitespace by trim())

    public class StringTestCase {
        @Test
        public void testSame() {
            String s = "\t abc \n";
    
            assertEquals("abc", s.trim());
            assertEquals("abc", s.strip());
        }
    
        @Test
        public void testDifferent() {
            Character c = '\u2000';
            String s = c + "abc" + c;
    
            assertTrue(Character.isWhitespace(c));
            assertEquals(s, s.trim());
            assertEquals("abc", s.strip());
        }
    }
    

提交回复
热议问题