< 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 are trying to remove all whitespace characters present at the beginning and at the end of it of a string value by calling the trim() method.

// Example of trim() method


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.




Strings are immutable but reference variables are mutable.


String objects are immutable, but the reference variables pointing to them are not, therefore in order to display the result of calling the trim() method on the original object, we can make the reference variable to the original string, point to the trimmed string value returned by the trim() method(but in this case, the original string will be lost).

// Example of trim() method

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





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter




< Prev
Next >
< concat() method
startsWith() method >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



Please check our latest addition

PYTHON