STRING INSTANCE METHODS

Java String strip() method

strip() removes Java whitespace from the beginning and end of a string. The interior stays unchanged and the result is returned as a String value. The original string is unchanged.

Removing whitespace from both ends

String tekstas = "  JavaQuest  ";
String rezultatas = tekstas.strip();

System.out.println("[" + rezultatas + "]");
System.out.println(tekstas.length());
System.out.println(rezultatas.length());
Console output

[JavaQuest]
13
9

  1. tekstas has two ordinary spaces before and after the name.
  2. tekstas.strip() returns the result without those four spaces. We save it in rezultatas.
  3. The original length is 13, and the result length is 9. The name itself is unchanged.
  4. The output brackets make it easier to see whether spaces remain at the edges. They are not part of the string returned by the method.

An instance method and an immutable string

strip() is called on a particular string and takes no arguments. The dot connects the call to the object whose beginning and end we want to process.

String tekstas = "  Java  ";

tekstas.strip();

System.out.println("[" + tekstas + "]");
Console output

[  Java  ]

Calling the method alone does not change the variable. This example does not save the returned result, so it prints the original string with all four spaces.

Method syntax

public String strip()

Parts of strip()
PartMeaning
Calling objectString — the string whose edge whitespace is removed.
ParametersThere are no arguments. Do not pass a delimiter, index or list of characters.
Return typeString
Original textUnchanged. The returned result can be saved or used in another call.
Java versionJava 11+
ErrorNullPointerException — when the method is called on a null reference.

The edge characters removed are those for which Character.isWhitespace(int) returns true. This includes more than the ordinary space character.

Save the returned result

String tekstas = "  Java  ";

tekstas = tekstas.strip();

System.out.println("[" + tekstas + "]");
System.out.println(tekstas.length());
Console output

[Java]
4

tekstas = tekstas.strip() assigns the returned value to the same variable. After assignment the variable refers to the string without edge whitespace. This does not make String objects mutable.

Spaces, tabs and line breaks

String tekstas = "\t\n Java \r\n";
String rezultatas = tekstas.strip();

System.out.println("[" + rezultatas + "]");
System.out.println(rezultatas.length());
Console output

[Java]
4

The beginning contains a tab \t, a line feed \n and an ordinary space. The end also contains a space and line-break characters, including \r. All edge characters in this example satisfy Java’s whitespace rule, so they are removed.

The backslashes in the code introduce Java string-literal escapes. They are not literal letters t or n in the resulting string.

Internal spaces and tabs remain

String tekstas = "  Java  Quest  ";
String tabai = "\tJava\tQuest\t";

System.out.println("[" + tekstas.strip() + "]");
System.out.println(tabai.strip().replace("\t", "(TAB)"));
Console output

[Java  Quest]
Java(TAB)Quest

The two spaces between Java and Quest remain. The method neither collapses them to one space nor removes all whitespace from the string.

In the second result, the internal tab is displayed as (TAB). That marker is inserted by a separate replace() call to make the output readable. The strip() call does not add it.

strip(), stripLeading() and stripTrailing()

String tekstas = "  Java Quest  ";

System.out.println("[" + tekstas.strip() + "]");
System.out.println("[" + tekstas.stripLeading() + "]");
System.out.println("[" + tekstas.stripTrailing() + "]");
Console output

[Java Quest]
[Java Quest  ]
[  Java Quest]

Which end should be processed?
The methodPurpose
strip()Removes whitespace from the beginning and end.
stripLeading()Removes whitespace only from the beginning.
stripTrailing()Removes whitespace only from the end.

The space between the words remains in all three results. The second line retains two trailing spaces, while the third retains two leading spaces. The brackets make them visible.

Practical example: preparing an entered name

String ivestis = " \t Java Quest \n";
String vardas = ivestis.strip();

if (vardas.isEmpty()) {
  System.out.println(false);
} else {
  System.out.println(vardas);
}

System.out.println(ivestis.equals(vardas));
Console output

Java Quest
false

  1. ivestis.strip() removes accidental whitespace only from the edges. The internal space between the two words stays.
  2. vardas.isEmpty() checks whether the string is empty after stripping. The name is not empty in this example, so its value is printed.
  3. The last result is false, because the original input and prepared name have different values. The original input did not change automatically.

This is simple input preparation. The method does not check allowed characters, name length or other application rules.

Empty input, whitespace-only input and unchanged text

String tuscias = "".strip();
String tarpai = " \t\n ".strip();

System.out.println("[" + tuscias + "]");
System.out.println(tuscias.length());
System.out.println("[" + tarpai + "]");
System.out.println(tarpai.length());
Console output

[]
0
[]
0

An empty string stays empty. When the whole string consists of Java whitespace, removing it also produces the empty string "". Both results have length zero.

String tekstas = "Java";
String rezultatas = tekstas.strip();

System.out.println("[" + rezultatas + "]");
System.out.println(rezultatas.equals(tekstas));
Console output

[Java]
true

If there are no removable characters at the edges, the content value is unchanged. Here equals() compares values. This is not an object identity check.

Compared with trim(): Unicode spaces and control characters

String tekstas = "\u2003Java\u2003";

System.out.println(Character.isWhitespace(0x2003));
System.out.println(tekstas.trim().length());
System.out.println(tekstas.strip().length());
System.out.println("[" + tekstas.strip() + "]");
Console output

true
6
4
[Java]

U+2003 is the whitespace character EM SPACE. It matches the rule tested by Character.isWhitespace(int) so strip() removes both edge characters, while trim() leaves them: the lengths are four and six respectively.

trim() treats every edge character from U+0000 through U+0020 inclusive as space. That is a different rule, so the methods are not completely interchangeable.

String tekstas = (char) 0 + "Java" + (char) 0;

System.out.println(tekstas.strip().length());
System.out.println(tekstas.trim().length());
System.out.println(Character.isWhitespace(0));
Console output

6
4
false

Here (char) 0 creates the NUL control character, not the digit 0. It is not Java whitespace. This time strip() keeps the six-character string, while trim() removes the two edge control characters. Lengths are printed so the invisible character does not need to be displayed.

Not every invisible character is removed

String tekstas = "\u00A0Java\u00A0";
String rezultatas = tekstas.strip();

System.out.println(Character.isWhitespace(0x00A0));
System.out.println(rezultatas.length());
System.out.println(rezultatas.equals(tekstas));
Console output

false
6
true

U+00A0 is a non-breaking space NBSP. For this character, Character.isWhitespace(int) returns false, so both edge characters remain. The result length is six and its value equals the original.

The same whitespace rule excludes the non-breaking spaces U+2007 and U+202F. The zero-width space U+200B is not Java whitespace either. A character being hard to see does not mean that strip() will remove it.

Multiline text is not stripped line by line

String tekstas = "  Java  \n  Quest  ";
String rezultatas = tekstas.strip();

System.out.println("[" + rezultatas.replace("\n", "(LF)") + "]");
Console output

[Java  (LF)  Quest]

Only the whitespace at the beginning and end of the whole string is removed. The two spaces before and two after the internal line break remain. The internal \n is displayed in the output as (LF) using replace(). The line break after the strip() call is still present.

To examine lines separately, use lines().

strip(), isBlank() and isEmpty() return different things

String tekstas = " \t\n ";

System.out.println(tekstas.isEmpty());
System.out.println(tekstas.isBlank());
System.out.println(tekstas.strip().isEmpty());
Console output

false
true
true

strip() returns a string. The methods isBlank() and isEmpty() return boolean. The original string contains characters, so its length is not zero, but all its characters are Java whitespace.

If you only need to know whether a string is empty or contains only Java whitespace, use isBlank(). When you also need the prepared value, save the result of strip() instead.

isBlank() · isEmpty()

Calling the method on null and checking safely

String tekstas = null;

System.out.println(tekstas.strip());
Result

NullPointerException

null does not refer to a string object, so the call throws NullPointerException. This differs from the empty string "", which the method can process.

String tekstas = null;

if (tekstas != null) {
  System.out.println("[" + tekstas.strip() + "]");
} else {
  System.out.println(false);
}
Console output

false

Check the reference first. When it is null, the example prints false and does not call the method. This is an application choice, not the result of strip() itself.

Common mistakes

  • Discarding the returned result and expecting the original string to change.
  • Expecting spaces between words or at the edges of every line to be removed.
  • strip() is treated as identical to trim().
  • Expecting non-breaking spaces or every invisible character to be removed.
  • Confusing text preparation with testing for an empty value or validating the whole input.
  • Calling the method on a null reference.

When should you use strip()?

strip() is useful for preparing text input, command values or fields when edge Java whitespace is insignificant under the application’s rules. Save the returned result and check the remaining value rules separately.

If one edge must be preserved, choose stripLeading() or stripTrailing(). When whitespace is meaningful content, keep it.