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 Java String trim with an example.

The basic syntax of the Java string trim is shown below.

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

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

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

  • If the String_Object represents an empty character sequence, then a 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), the reference of this String_Object will return.
  • When there is no character with a code greater than ‘\u0020’ (the space character), the Java trim function creates and returns an empty string.
  • 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 will create 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 trim method.

First, we declared the String variable Str and assigned the corresponding value.

The first statement will remove the white spaces from both the Left and Right sides of Str using the Java string.trim function.

Use this function for trimming the left-hand side or leading whitespaces. The following str2 statement will remove white spaces from the left-hand side.

Use Java string.trim function for removing the Right-hand side or trailing whitespaces. The following st3 statement will remove white spaces from the right side.

For the next three variables str4, str5, and str6, we used the tabs and line terminator along with the Java objects. If you observe the below screenshot, the String Method is removing the terminators like \t and \n.

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

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