Java String strip() method with examples

strip() method was introduced in Java 11, and it helps remove the leading and trailing whitespaces from the string. In this post, we will discuss the strip() method in detail and see how it is different from the trim() method.

  • Method declaration – public String strip()
  • What does it do? – It will remove all the leading and trailing whitespaces from the string.
  • What does it return? It will return a new String object with all the leading and trailing whitespaces removed, but it will return the same String reference if no whitespaces are removed.
What will be considered whitespace in the strip() method?

strip() method internally uses the isWhitespace(int ch) of the Character class to see whether the character is whitespace or not, and according to the Java documentation

A character is a Java whitespace character if and only if it satisfies one of the following criteria:

  • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR)but is not also a non-breaking space (‘\u005Cu00A0’, ‘\u005Cu2007’, ‘\u005Cu202F’).
  • ‘\u005Ct’, U+0009 HORIZONTAL TABULATION.
  • ‘\u005Cn’, U+000A LINE FEED.
  • ‘\u005Cu000B’, U+000B VERTICAL TABULATION.
  • ‘\u005Cf’, U+000C FORM FEED.
  • ‘\u005Cr’, U+000D CARRIAGE RETURN.
  • ‘\u005Cu001C’, U+001C FILE SEPARATOR.
  • ‘\u005Cu001D’, U+001D GROUP SEPARATOR.
  • ‘\u005Cu001E’, U+001E RECORD SEPARATOR.
  • ‘\u005Cu001F’, U+001F UNIT SEPARATOR.

So, if any of the above conditions are met, it will be considered whitespace, and the strip() method will remove that whitespace from both sides.

Code Example
public class Codekru {

	public static void main(String[] args) {

		String str1 = "  codekru  ";
		System.out.println("Before using strip fucntion: " + "====" + str1 + "=====");

		String str2 = str1.strip();
		System.out.println("After using strip fucntion: " + "====" + str2 + "=====");

	}
}

Output –

Before using strip fucntion: ====  codekru  =====
After using strip fucntion: ====codekru=====

You can see that space is removed after using the strip() method, so if you want to remove the leading and trailing whitespaces from a string, then the strip() method is the one we recommend.

Some of you might think that there is one more function named trim(), which also does the same thing as strip(). So, what is the difference between strip() and trim() function?

Difference between trim() vs. strip() method

The major difference between them is that the trim() function removes the spaces whose code point is less than or equal to ‘U + 0020’. So it won’t be able to remove all kinds of whitespaces like what we have used in our code before ( ‘U + 2003’ ), whereas the strip() method can be used to remove the variety of whitespaces as specified above. So, if you want to remove whitespaces from a string, then strip() is the function you might want to use.

Below is the code example showing the difference between the trim() and the strip() method –

public class Codekru {

	public static void main(String[] args) {

		String str1 = '\u2003' + "codekru";
		System.out.println("Before using strip fucntion: " + "====" + str1 + "=====");

		String str2 = str1.trim();
		System.out.println("After using trim fucntion: " + "====" + str2 + "=====");

		String str3 = str1.strip();
		System.out.println("After using strip fucntion: " + "====" + str3 + "=====");

	}
}

Output –

Before using strip fucntion: ==== codekru=====
After using trim fucntion: ==== codekru=====
After using strip fucntion: ====codekru=====

The What If scenarios

What if you only want to remove the leading whitespaces, means spaces present at the beginning of a string?

Java has provided us with one function called strimLeading(), which removes only the leading whitespaces from the string, as shown below in the program.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "  codekru  ";
		System.out.println("Before using stripLeading fucntion: " + "====" + str1 + "=====");

		String str2 = str1.stripLeading();
		System.out.println("After using stripLeading fucntion: " + "====" + str2 + "=====");

	}
}

Output –

Before using strip fucntion: ====  codekru  =====
After using strip fucntion: ====codekru  =====
Now, what if you only want to remove the trailing spaces, which means spaces at the string’s end?

Yeah, you guessed it right. Java provides a function named stripLeading() which only removes the trailing whitespaces as shown in the below program.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "  codekru  ";
		System.out.println("Before using stripTrailing fucntion: " + "====" + str1 + "=====");

		String str2 = str1.stripTrailing();
		System.out.println("After using stripTrailing fucntion: " + "====" + str2 + "=====");

	}
}

Output –

Before using stripTrailing fucntion: ====  codekru  =====
After using stripTrailing fucntion: ====  codekru=====
What if we try to use the strip() method on a string containing spaces only?

In this case, the method will return an empty string by stripping away all whitespaces. Remember, the string will be empty and not null.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "   ";
		System.out.println("Before using strip fucntion: " + "====" + str1 + "=====");

		String str2 = str1.strip();
		System.out.println("After using strip fucntion: " + "====" + str2 + "=====");

		System.out.println("is str2 empty? " + str2.isEmpty());

	}
}

Output –

Before using strip fucntion: ====   =====
After using strip fucntion: =========
is str2 empty? true
What if we try to use the strip() method on a null String?

We will get a NullPointerException if we try to do this, so we don’t recommend doing it 😛

public class Codekru {

	public static void main(String[] args) {

		String str1 = null;

		String str2 = str1.strip();
		System.out.println("After using strip fucntion: " + "====" + str2 + "=====");

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.strip()" because "str1" is null
Does the strip() function return the same reference if no whitespaces are removed?

Yeah, this is true. We can prove this by using the == operator to check whether the strings refer to the same string before and after using the strip() function.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = str1.strip();

		if (str1 == str2) {
			System.out.println("Both are pointing to the same string reference");
		} else {
			System.out.println("Both are pointing to the different string references");
		}

	}
}

Output –

Both are pointing to the same string reference

If you want to know more about strings and their methods, we recommend giving this article a read.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *