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

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


In short: strip() is "Unicode-aware" evolution of trim(). Meaning trim() removes only characters <= U+0020 (space); strip() removes all Unicode whitespace characters (but not all control characters, such as \0)

CSR : JDK-8200378

Problem

String::trim has existed from early days of Java when Unicode

had not fully evolved to the standard we widely use today.

The definition of space used by String::trim is any code point lessthan or equal to the space code point (\u0020), commonly referred toas ASCII or ISO control characters.

Unicode-aware trimming routines should useCharacter::isWhitespace(int).

Additionally, developers have not been able to specifically removeindentation white space or to specifically remove trailing whitespace.

Solution

Introduce trimming methods that are Unicode white space awareand provide additional control of leading only or trailing only.

A common characteristic of these new methods is that they use a different (newer) definition of "whitespace" than did old methods such as String.trim(). Bug JDK-8200373.

The current JavaDoc for String::trim does not make it clear whichdefinition of "space" is being used in the code. With additionaltrimming methods coming in the near future that use a differentdefinition of space, clarification is imperative. String::trim usesthe definition of space as any codepoint that is less than or equal tothe space character codepoint (\u0020.) Newer trimming methods willuse the definition of (white) space as any codepoint that returns truewhen passed to the Character::isWhitespace predicate.

The method isWhitespace(char) was added to Character with JDK 1.1, but the method isWhitespace(int) was not introduced to the Character class until JDK 1.5. The latter method (the one accepting a parameter of type int) was added to support supplementary characters. The Javadoc comments for the Character class define supplementary characters (typically modeled with int-based "code point") versus BMP characters (typically modeled with single character):

The set of characters from U+0000 to U+FFFF is sometimes referred toas the Basic Multilingual Plane (BMP). Characters whose code pointsare greater than U+FFFF are called supplementary characters. The Javaplatform uses the UTF-16 representation in char arrays and in theString and StringBuffer classes. In this representation, supplementarycharacters are represented as a pair of char values ... A char value,therefore, represents Basic Multilingual Plane (BMP) code points,including the surrogate code points, or code units of the UTF-16encoding. An int value represents all Unicode code points, includingsupplementary code points. ... The methods that only accept a charvalue cannot support supplementary characters. ... The methods thataccept an int value support all Unicode characters, includingsupplementary characters.

OpenJDK Changeset.


Benchmark comparison between trim() and strip() - Why is String.strip() 5 times faster than String.trim() for blank string In Java 11


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());    }}


In general both method removes leading and trailing spaces from string. However the difference comes when we work with unicode charaters or multilingual features.

trim() removes all leading and trailing character whose ASCII value is less than or equal to 32 (‘U+0020’ or space).

According to Unicode standards there are various space characters having ASCII value more than 32(‘U+0020’). Ex: 8193(U+2001).

To identify these space characters, new method isWhitespace(int) was added from Java 1.5 in Character class. This method uses unicode to identify space characters. You can read more about unicode space characters here.

New method strip which is added in java 11 usage this Character.isWhitespace(int) method to cover wide range of white space characters and remove them.

example

public class StringTrimVsStripTest {    public static void main(String[] args) {        String string = '\u2001'+"String    with    space"+ '\u2001';        System.out.println("Before: \"" + string+"\"");        System.out.println("After trim: \"" + string.trim()+"\"");        System.out.println("After strip: \"" + string.strip()+"\"");   }}

Output

Before: "  String    with    space  "After trim: " String    with    space "After strip: "String    with    space"

Note: If you are running on windows machine, you may not be able to see the similar output due to limited unicode set. you can try some online compilers for testing this code.

reference: Difference between trim and strip method java