Java – String trim() method

In this article, we will discuss String’s trim() method which is used remove both leading and trailing white-spaces

1. String’s trim() method:

  • This String method is used to remove trailing as well as leading white spaces from the invoking-string

1.1 Method Signature:

public String trim();

1.2 Returns:

  • Return a String after removing both leading and trailing white spaces

2. Examples on trim() method:

In the below example,

  • We will create 2 String objects and 2 String literals
  • Print to console without invoking trim() method on any Strings
  • Later, we will invoke trim() method on all 4 strings
  • Finally, printing again the string to console

2.1 To trim both leading and trailing spaces

  • to trim leading/trailing white-spaces from the invoking-string
  • i.e.; to remove/eliminate white-spaces on both sides

StringTrimMethod.java

package in.bench.resources.string.methods;

public class StringTrimMethod {

	public static void main(String[] args) {

		// create 2 String objects
		String strObject1 = new String("    this string object "
				+ "is with leading white-space");
		String strObject2 = new String("    this string  object "
				+ "has both leading and "
				+ "trailing white-spaces      ");

		// create 2 String literals
		String strLiteral3 = "this string literal is with "
				+ "trailing white-space      ";
		String strLiteral4 = "    this string  literal "
				+ "has both leading and "
				+ "trailing white-spaces      ";

		// lets print all strings without trim() method
		System.out.println("All Strings without invoking "
				+ "trim() method\n");
		System.out.println("String Object 1 contents  : "
				+ strObject1);
		System.out.println("String Object 2 contents  : "
				+ strObject2);
		System.out.println("String Literal 3 contents : "
				+ strLiteral3);
		System.out.println("String Literal 4 contents : "
				+ strLiteral4);

		// lets print all strings without trim() method
		System.out.println("\n\nAll Strings after invoking "
				+ "trim() method "
				+ "to remove white-spaces\n");
		System.out.println("String Object 1 contents  : "
				+ strObject1.trim());
		System.out.println("String Object 2 contents  : "
				+ strObject2.trim());
		System.out.println("String Literal 3 contents : "
				+ strLiteral3.trim());
		System.out.println("String Literal 4 contents : "
				+ strLiteral4.trim());
	}
}

Output:

All Strings without invoking trim() method

String Object 1 contents  :     this string
		object is with leading white-space
String Object 2 contents  :     this string
		object has both leading and trailing white-spaces
String Literal 3 contents : this string
		literal is with trailing white-space
String Literal 4 contents :     this string
		literal has both leading and trailing white-spaces      

All Strings after invoking trim() method to remove white-spaces

String Object 1 contents  :
	this string object is with leading white-space
String Object 2 contents  :
	this string  object has both leading and trailing white-spaces
String Literal 3 contents :
	this string literal is with trailing white-space
String Literal 4 contents :
   this string  literal has both leading and trailing white-spaces

Hope, you found this article very helpful. If you have any suggestions or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String valueOf() method
Java - String toUpperCase() method