- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - String stripTrailing() Method
The Java String stripTrailing() method is used to remove the whitespaces from the end of a String. It retrieves a copy of the string with trailing spaces omitted. The same string is retrieved if it has no trailing whitespace.
This method results in an empty string if the given string contains only white spaces. Similar to thestrip() method, the stripTrailing() methodcan also remove space characters as per the Unicode standards having ASCII value greater than 32(\u0020).
Syntax
Following is the syntax for Java String stripTrailing() method −
public String stripTrailing()
Parameters
This method does not accept any parameter.
Return Value
This method returns a copy of this string with trailing white space removed, or this string if it has no trailing white space.
Example
The following example shows the usage of Java String stripTrailing() method. Here we are trying to remove the trailing white spaces from the string "This is TutorialsPoint " −
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
// string with trailing white space
String str = "This is TutorialsPoint ";
System.out.print("Before removing spaces = ");
System.out.println(".." + str + "..");
// trailing white space removed
System.out.print("After removing spaces = ");
System.out.println(".." + str.stripTrailing() + "..");
}
}
Output
If you compile and run the above program, it will produce the following result −
Before removing spaces = ..This is TutorialsPoint .. After removing spaces = ..This is TutorialsPoint..
Example
Below is an example to remove all the trailing spaces using stripTrailing() method in Java −
import java.util.Locale;
public class StringDemo {
public static void main(String[] args) {
String str = " Tutorials Point ";
System.out.println("The length of the string before removing spaces is: " + str.length());
System.out.println("The string without removing spaces is: " + str);
String t = str.stripTrailing();
System.out.println("The length of the string after removing trailing spaces is: " + t.length());
System.out.println("The string with removed spaces is:" + t);
}
}
Output
If you compile and run the program above, the output will be displayed as follows −
The length of the string before removing spaces is: 25 The string without removing spaces is: Tutorials Point The length of the string after removing trailing spaces is: 19 The string with removed spaces is: Tutorials Point
Example
In the code given below the stripTrailing() method is invoked on two strings 's1' and 's2' respectively. The second string has a space character with a codepoint value > 32(\u0020). Then the spaces present at the end of the string gets removed, whereas the ones at the beginning doesn't −
public class StringDemo {
public static void main(String[] args) {
String s1 = " Java Programming ";
System.out.println("The first string given is: " + ".." + s1 + "..");
// string with the whitespace character as per the Unicode values
String s2 = " Java Programming" + '\u2003';
System.out.println("The second string is: " + ".." + s2 + "..");
System.out.println("The first string after removing trailing spaces is : " + ".." + s1.stripTrailing() + "..");
System.out.println("The second string after removing trailing spaces is: " + ".." + s2.stripTrailing() + "..");
}
}
Output
On executing the program above, the output is obtained as follows −
The first string given is: .. Java Programming .. The second string is: .. Java Programming?.. The first string after removing trailing spaces is : .. Java Programming.. The second string after removing trailing spaces is: .. Java Programming..
Example
After trailing whitespace is removed from a string using the stripTrailing() method, a new string is returned since strings in Java are immutable. The reference to the same string is returned if the manipulation is not carried out by this method −
public class StringDemo {
public static void main(String argvs[]) {
String s = "Tutorials Point ";
System.out.println("The given string is: " + ".." + s + "..");
String s1 = s.stripTrailing();
System.out.println("The string after removing trailing whitespace is: " + ".." + s1 + "..");
// the hashcode of s and s1 is different
System.out.println("The hashcode of string s is: " + s.hashCode());
System.out.println("The hashcode of string s1 is: " + s1.hashCode());
String x = "calm";
String y = x.stripTrailing();
// the hashcode of x and y is the same
System.out.println("The hashcode of string x is: " + x.hashCode());
System.out.println("The hashcode of string y is: " + y.hashCode());
}
}
Output
The output of the above program is as follows −
The given string is: ..Tutorials Point .. The string after removing trailing whitespace is: ..Tutorials Point.. The hashcode of string s is: 882187205 The hashcode of string s1 is: -1622622651 The hashcode of string x is: 3045983 The hashcode of string y is: 3045983