Java trim Method

The Java trim Method is one of the String Methods, which is useful to remove the empty spaces from both the Right-hand side and Left-hand side of a string. In this article, we will show how to use String trim in Java Programming language with example. The basic syntax of the string trim in Java Programming language is as shown below.

public String trim(); // It will return String 

//In order to use in program
String_Object.trim()

String_Object: The Java string.trim Function will return the copy of this string with leading and trailing white spaces removed. Please specify the valid String Object for Java trim function.

  • If the String_Object represents an empty character sequence, then reference to this String_Object is returned.
  • If both the first & last character of the String_Object has codes greater than ‘\u0020’ (the space character), then reference of this String_Object is returned.
  • When there is no character with a code greater than ‘\u0020’ (the space character), then an empty string is created and returned.
  • Let x be the index of the First character and y be the index of the Last character of the String_Object have codes greater than ‘\u0020’. A new string created which represents the substring of the String_Object that begins at index x and ends at index y. We can also say, this.substring(x, y+1).

Java trim Method Example

The Java string trim method removes the leading and trailing empty spaces or white spaces. This program will help to understand the string.trim method.

package StringFunctions;

public class TrimMethod {
 public static void main(String[] args) {
 String str = "   Free Java Tutorial at Tutorial GateWay   ";
 
 String str1 = str.trim();
 String str2 = "    Tutorial Gateway".trim();
 String str3 = "Java Programming     ".trim();
 String str4 = "\t Let us see the tab Space";
 String str5 = str4.trim();
 String str6 = "\n Let us see the tab Space \n";
 
 System.out.println( "Trimmed String = " + str1);
 System.out.println( "Trimmed String = " + str2);
 System.out.println( "Trimmed String = " + str3);
 System.out.println( "Before Trimming str4 String = " + str4);
 System.out.println( "After Trimming str4 String = " + str5);
 System.out.println( "Before Trimming str6 String = " + str6);
 System.out.println( "After Trimming str6 String = " + str6.trim());
 }
}
Java trim Method 1

First, we declared the String variable Str and assigned corresponding value using the following statement

String str = "   Free Java Tutorial at Tutorial GateWay   ";

The following statement will remove the white spaces from both the Left and Right side of String variable Str using Java string.trim function

String str1 = str.trim();

Use string trim function for trimming the left-hand side of a given string. The following statement will remove white spaces from the left-hand side.

String str2 = "    Tutorial Gateway".trim();

Use Java string.trim function for trimming the Right-hand side of a specified string. The following statement will remove white spaces from the right side.

String str3 = "Java Programming     ".trim();

Next, We used the tabs and line terminator along with the Java string objects. If you observe the above screenshot, the trim String Method is trimming the terminators like \t and \n.

String str4 = "\t Let us see the tab Space";
String str5 = str4.trim();
String str6 = "\n Let us see the tab Space \n";

Lastly, we used the System.out.println statements to print the trimmed data (output).