String Class stripTrailing() Method in Java with Examples Last Updated : 30 Jul, 2024 Comments Improve Suggest changes Like Article Like Report This method is used to strip trailing whitespaces from the string i.e. stripTrailing() method removes all the whitespaces only at the ending of the string. Example:Input: String name = " kapil "; System.out.println("#" + name + "#"); System.out.println("#" + name.stripTrailing() + "#"); Output: # kapil # # kapil# // trailing whitespaces are removed Explanation:name.stripTrailing(): This method removes trailing whitespaces from the string name.The output correctly shows:The first line with leading and trailing spaces intact: # kapil #.The second line with trailing spaces removed: # kapil#.Syntax:public String stripTrailing()Parameter: It accepts no parameters.Returns: The string after removing all the whitespaces at the end of the string.Note: Like the stripTrailing() method in java, we have strip() (removes leading and trailing whitespace) and stripLeading() (removes the only leading whitespace). Java // Java program to demonstrate the usage of // stripTrailing() method in comparison to // other methods class GFG { public static void main(String[] args) { // creating a string String str = " Hello, World "; // print the string without any function System.out.println("String is"); System.out.println("#" + str + "#"); System.out.println(); // using strip() method System.out.println("Using strip()"); System.out.println("#" + str.strip() + "#"); System.out.println(); // using stripLeading() method System.out.println("Using stripLeading()"); System.out.println("#" + str.stripLeading() + "#"); System.out.println(); // using stripTrailing() method System.out.println("Using stripTrailing()"); System.out.println("#" + str.stripTrailing() + "#"); } } OutputString is # Hello, World # Using strip() #Hello, World# Using stripLeading() #Hello, World # Using stripTrailing() # Hello, World# Explanation of the above Program:This Java program demonstrates the use of the strip(), stripLeading(), and stripTrailing() methods to manipulate whitespaces in a string.Original String: The string " Hello, World " is printed with leading and trailing whitespaces intact.Using strip(): This method removes both leading and trailing whitespaces from the string.Using stripLeading(): This method removes only the leading (left-side) whitespaces.Using stripTrailing(): This method removes only the trailing (right-side) whitespaces. Comment More infoAdvertise with us Next Article String Class stripTrailing() Method in Java with Examples K kapilm180265ca Follow Improve Article Tags : Java Java-Strings Practice Tags : JavaJava-Strings Similar Reads Java String Class indent() Method With Examples JDK 12 introduced indent() method in Java.lang.String class. This method is useful to add or remove white spaces from the beginning of the line to adjust indentation for each string line. Syntax: public String indent(int n) Parameter: It takes integer n as input and does indentation accordingly. Al 3 min read StringBuffer toString() method in Java with Examples The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Sub 2 min read StringBuilder toString() method in Java with Examples The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString( 3 min read StringBuffer substring() method in Java with Examples In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The strin 4 min read StringBuffer trimToSize() method in Java with Examples The trimToSize() method of StringBuffer class is the inbuilt method used to trims the capacity used for the character sequence of StringBuffer object. If the buffer used by StringBuffer object is larger than necessary to hold its current sequence of characters, then this method is called to resize t 2 min read Scanner toString() method in Java with Examples The toString() method of java.util.Scanner class returns the string representation of this Scanner. The exact format is unspecified. Syntax: public String toString() Return Value: This function returns the string representation of this scanner. Below program illustrates the above function: Program 1 1 min read String replace() method in Java with Examples The String replace() method returns a new string after replacing all the old characters/CharSequence with a given character/CharSequence. Example:Return a new string where all " o" characters are replaced with "p" character: Java // Java program to demonstrate // the replace() method public class Ma 4 min read StringBuilder trimToSize() method in Java with Examples The trimToSize() method of StringBuilder class is the inbuilt method used to trims the capacity used for the character sequence of StringBuilder object. If the buffer used by StringBuilder object is larger than necessary to hold its current sequence of characters, then this method is called to resiz 2 min read Pattern split(CharSequence) method in Java with Examples split(CharSequence) method of a Pattern class used to splits the given char sequence passed as parameter to method around matches of this pattern.This method can split charSequence into an array of String's, using the regular expression used to compile the pattern as a delimiter.so we can say that t 2 min read Pattern quote(String) method in Java with Examples quote(String) method of a Pattern class used to returns a literal pattern String for the specified String passed as parameter to method.This method produces a String equivalent to s that can be used to create a Pattern. Metacharacters or escape sequences in the input sequence will be given no specia 3 min read Like