< Prev
Next >



trim() method




The trim() method of String class is used to trim all the whitespace characters present at the beginning and at the end of a string.




Signature of trim()


public String trim()






Method trim() example


Here in program below, we have a String object that contains a string value with many whitespaces at the beginning and at the end of it and we are trying to trim all these leading and trailing whitespaces it by calling trim() method on it.
// Program to trim the leading and trailing blank spaces from a String.


class StringTrim
{
public static void main(String[] ar)
{
String str1= new String("         hello            ");
System.out.println("String value is : "  + str1);
System.out.println("The trimmed String value is : " + str1.trim());

}

}



Output is :



String value is :          hello
The trimmed String value is : hello





Method trim() does not modify the original content in a String object


The method trim() only returns the content of String object, minus the leading and trailing whitespaces in it. Method trim() does not modify the original content of a String object. Let's understand this by an example.


Here in program below, we have initialized a String object with a value new york and we are trying to remove all whitespace characters present at the beginning and at the end of it by calling trim() method.
//Program to convert a String to lower case letters. 


class StringUpper
{
public static void main(String[] ar)
{
String str1= new String("       how is your day going on?            ");
System.out.println("String value is : "  + str1);
System.out.println("The trimmed String value is : " + str1.trim());

System.out.println("String value : " + str);
}

}



Output is :


String value is :          how is your day going on?
The trimmed String value is : how is your day going on?
String value is :          how is your day going on?

The String object contains the same original value even after the method trim() was called on it, because the method trim() only returns the content of String object, minus the leading and trailing whitespaces in it. It does not modify the original content of a String object.




Modifying the content of a String object with leading and trailing whitespaces.


In the upcoming code, we are modifying the content of the invoked String object by storing the trimmed String value returned by trim() method, back into the String object, using its reference variable.
//Program to convert a string to lower case letters. 

class StringUpper2
{
public static void main(String[] ar)
{
String str= new String("       mother nature should be protected      ");
System.out.println("String value is : "  + str);

str = str.trim(); //result of calling trim() is stored back in String object

System.out.println("String's trimmed value : " + str);
}
}



Output is :


String value is :       mother nature should be protected
String's trimmed value : mother nature should be protected

The original content of String object is modified by placing the result of trim() method back in the original String object.



Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< concat() method
startsWith() method >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2