Java String trim() Method

A builtin function that eliminates leading and trailing spaces is the trim function of Java String. A space character’s Unicode value is “u0020”. Before or after a string, this Unicode value is checked by the trim() method in Java and spaces are removed if necessary.

 

Method Signature

public String trim()

Return Type

Trim in Java():
import java.io.*;
class techvidvan  {
public static void main(String[] args) {
String x="tech ";
String y="for ";
String z="vidvan";
System.out.println(x+y+z);
System.out.println(x.trim()+y.trim()+z.trim());
}}

Method signature

public String trim()

Parameters

The trim() method takes no parameters.

Return Type

The return type of the trim() method is String. Returns an omitted string without leading and trailing spaces.
Below are examples of how Java’s string trim() method works.

Example for Trim

Method signature

public String trim()

Parameters

The trim() method has no parameters.

Return Type

The return type of the trim() method is String. Returns an escaped string without leading and trailing spaces.
Below are examples of how Java’s string trim() method works.

Parameters

None.

Returns:

A string value is a copy of the string without leading and trailing spaces.

Syntax of trim() in Java

public String trim().

Example for Trim

Example 1

import java.io.*;
class techvidvan  {
public static void main(String[] args) {
String x="tech ";
String y="for ";
String z="vidvan";
System.out.println(x+y+z); with
System.out.println(x.trim()+y.trim()+z.trim());
}
}

Output

tech for vidvan
techforvidan

Example 2

After using the trim function, it returns a string instead of changing the original string.

class techvidvan  {
public static void main(String args[])
{
String s = " techvidvan  has all Java features to read ";
System.out.println(s.trim());
System.out.println(s);
}
}

Output

tech for vidvan has all java functions to read
tech for vidvan has all java functions to read

Example 3

Using the trim function, we get two strings the original and the returned string, both differ in the case that we remove spaces from the original string.

import java.io.*;
class techvidvan {
public static void main(String[] args)
{
String s1 = "techvidvan ";
System.out.println("Before Trim() - ");
System.out.println("String - " + s1);
System.out.println("Length - " + s1.length());
String s2 = s1.trim();
System.out.println("\nAfter Trim() - ");
System.out.println("String - " + s2);
System.out.println("Length - " + s2.length());
if (s1 == s2) {
System.out.println("\nEqual");
}
otherwise {
System.out.println("\nNot equal to");
}
}
}

Output

Before Trim() –
String – tech for vidvan
Length – 21

After Trim() –
String – tech for vidvan
Length – 15

Not Equal

Public String trim()

To obtain a string with the value of this string, leading and trailing spaces will be removed by using trim() method. A reference to this String object shall be returned if this String object contains an empty sequence of characters or the first and last characters of the sequence of characters represented by this String object have a code greater than ‘u0020’, a space character.

In the case of a string with no characters with code in excess of ‘u0020’, an empty string object is returned. Otherwise, k shall be the index of the first character in a string whose code is greater than ‘u0020’, and m shall be the index of the last character in this string whose code is greater than ‘u0020.

This method can be used to trim the spaces listed above from the beginning or end of each string.

Conclusion

The Java String trim() method removes lead and trailing spaces. No standard API is used to check if the characters are empty. For a character whose Unicode code falls below or is not equal to U+0020, this deletes it. Therefore, in Java 11, a method called strip() has been added. Use the strip() method if you use Java 11 or higher.