Java String.trim() removes leading and trailing characters whose Unicode value is less than or equal to U+0020. It does not remove whitespace from the middle of the string. This tutorial explains the method, its return value, examples, and when Java’s strip() method is a better choice.

How Java String.trim() removes leading and trailing characters

Use String.trim() when you want to remove qualifying characters from the beginning and end of a Java string.

The method returns a string whose leading and trailing characters with code points at or below U+0020 have been removed. This range includes common ASCII control characters such as tab, line feed, carriage return, form feed, and the ordinary space character.

trim() does not remove characters from the middle of the string. It also does not treat every Unicode whitespace character as removable whitespace. That distinction matters when your input can contain non-ASCII spaces.

Java String.trim() syntax and return value

Following is the syntax of trim() in the String class.

</>
Copy
public String trim()

The method takes no arguments and returns a String. Java strings are immutable, so trim() does not modify the original string. Assign or use the returned value if you need the trimmed text.

If the string has no leading or trailing characters that need to be removed, the visible text remains unchanged. If the string contains only removable characters, the result is an empty string.

Java trim() example with spaces, tabs, and new lines

1. Trim whitespaces from edges of a string

In the following Java program, we initialize a String with some white space characters like single space, new line and new tab. We shall then apply the trim() function to this string and observe the output.

Example.java

</>
Copy
public class Example {
	public static void main(String[] args) {
		//initialize string
		String str = "  \t\n
\tHello World!\n Welcome to www\.tutorialkart.com\n
\t";

		//trim string
		String result = str.trim();

		System.out.print("\""+result+"\"");
	}
}

Run the above program and you shall see something similar to the following in console window.

"Hello World!
 Welcome to www\.tutorialkart.com"

We have just added leading and trailing double quotes to understand the extent of the resulting string.

The leading and trailing white spaces are gone in the resulting string from trim() function.

Also, note that the white space characters inside the string are preserved. Like the white spaces and new line in this example.

What Java trim() leaves unchanged inside a String

trim() only works at the two edges of the string. Spaces, tabs, or line breaks between non-trimmed characters remain in place. For example, trimming " Java String " produces "Java String"; the three spaces between the words are not collapsed or removed.

If your goal is to replace or normalize whitespace inside the text, you need a different operation, such as a regular expression with replaceAll(). The trim() method is only for the beginning and end.

Java trim() when the String contains only removable whitespace

2. Trim String where the string has only whitespace characters in it

In the following Java program, we initialize a String with only white space characters and nothing else.

Example.java

</>
Copy
/**
 * Java Example Program to Trim white space characters at the edges of string
 */

public class Example {

	public static void main(String[] args) {
		//initialize string
		String str = "  \t     \n \n\t  \n \n  \t  \r  \f \n";

		//trim string
		String result = str.trim();

		System.out.print("\""+result+"\"");
	}
}

Run the above program, and you shall get the following in the console output.

Output

""

The resulting String is just an empty string, since there are no characters with unicode greater than ‘\u0020’.

What Java trim() does not do: trim to length or remove arbitrary characters

trim() is specifically an edge-whitespace operation under Java’s U+0020 rule. It does not shorten a string to a maximum number of characters, and it does not accept a character argument.

If you need only the first part of a string, use methods such as substring() with appropriate bounds checking. If you need to remove a particular character from the start or end, use logic designed for that character or a suitable regular expression rather than expecting trim() to do it.

Java trim() vs strip() for Unicode whitespace

The main difference between trim() and strip() is how Java decides what counts as whitespace. trim() uses the older rule of removing leading and trailing characters with values at or below U+0020. strip(), added in Java 11, uses Java’s Unicode-aware whitespace test.

For ordinary spaces, tabs, and line breaks, both methods often produce the same result. For text that may contain Unicode whitespace, strip() is generally the more appropriate choice when you are using Java 11 or later.

</>
Copy
String text = "   Java   ";

String withTrim = text.trim();
String withStrip = text.strip();

System.out.println(withTrim);
System.out.println(withStrip);
Java
Java

Using the value returned by String.trim()

Because String objects are immutable, calling trim() by itself does not change the variable’s existing value. Store the returned value, pass it directly to another method, or use it in an expression.

</>
Copy
String name = "  Amit  ";
name = name.trim();

System.out.println(name);
Amit

Also remember that trim() is an instance method. Calling it on a null reference causes a NullPointerException. Check for null first when the source value may be absent.

Java String.trim() behavior at a glance

  • Removes qualifying characters only from the start and end of the string.
  • Preserves spaces and other characters inside the string.
  • Returns a string instead of changing the original String object.
  • Returns an empty string when every character is removed.
  • Uses the legacy U+0000 through U+0020 trimming rule rather than a full Unicode whitespace definition.
  • Can be replaced with strip() in Java 11 or later when Unicode-aware whitespace handling is required.

For the method contract, see the Oracle Java String API documentation.

Java String.trim() summary

In this Java Tutorial, we learned how to trim white spaces in a string at the leading and trailing edges.