String trim()

In this post, we will read about the String trim() method and how to use the java trim method and remove space from string java. The trim() method is present in the String class of java.lang package. Let’s see the advantage of the trim() function in java.

Here is the table content of the article will we will cover this topic.
1. What is Java trim() method?
2. Syntax of java trim() method
3. How does the trim() method work?
4. Important points about the java trim method

What is Java trim() method?

String trim() method is used to truncate the leading space and trailing space from the string. It is used to remove space from string java, but it only removes the extra space before and after the string. As we know string is an array of characters, so each character stores in a particular index with its Unicode value.  
The Unicode value of space character is ‘\u0020’ so when we call trim() method it checks the Unicode of space character(‘\u0020’) only before and after of string. If the Unicode of a space character is existing, it removes the spaces and returns the string after the removal of spaces.

String trim in Java

Syntax of java trim() method

access modifier – The access modifier of the trim() method is public. So, can be accessible from everywhere.
return type: The return type of trim() method is String. It returns a String value after removing of white spaces.
Parameter: It doesn’t take any parameter.

String trim in Java
public class StringExample
{
	public static void main(String[] args) 
	{
		String str = " JavaGoal Webiste ";
		System.out.println("Before trim:"+ str);
		System.out.println("After trim:"+ str.trim());
	}
}

Output: Before trim: JavaGoal Webiste
After trim:JavaGoal Webiste

In above program, we have a String object str that contains a string(JavaGoal website) with white spaces at the beginning and at the end. We are removing those spaces by use of trim() method.

How does the trim() method work?

As we know String is an array of characters and each character is stroes in a particular index. Before saving the string JVM converts all characters in Unicode and then save it.

Suppose if we have a String “ JavaGoal  ”, then JVM will convert each character in Unicode and save in memory.  In this above string, we have three white space one space at the beginning, and two are at the end of the string. So let’s see how it will store in memory.

String trim in Java


When we will call the trim() method by use of string object. The trim() method finds the “\u0020” (White space) and removes the spaces from both ends. It doesn’t remove the white space between strings. The trim() method always returns a new string after removing space and the original string will remain the same , because the string is immutable in java.

String trim in Java

Important points about the java trim method

1. java trim method removes the white space from both ends. It doesn’t remove the white space between String.

2. Whenever we call the trim() method, it returns a new object of the string after removing of space and the original string object remains unchanged.

public class StringExample
{
	public static void main(String[] args) 
	{
		String str1 = " JavaGoal ";
		String str2 = str1.trim();
		System.out.println("After trim str1:"+str1);
		System.out.println("After trim str2:"+str2);
		System.out.println("Are both have same: "+ (str1 == str2));
	}
}

Output: After trim str1: JavaGoal
After trim str2:JavaGoal
Are both have same: false

3. Java trim method returns the same string object only if the string is empty, or there are no leading and trailing white spaces.

public class StringExample
{
	public static void main(String[] args) 
	{
		String str1 = "";
		String str2 = str1.trim();
		System.out.println("After trim str1:"+str1);
		System.out.println("After trim str2:"+str2);
		System.out.println("Are both have same: "+ (str1 == str2));
	}
}

Output: After trim str1:
After trim str2:
Are both have same: true

4. If a string containing only white spaces, then the trim() method returns the empty string.

Leave a Comment