Java String trim() Method
Last Updated :
27 Nov, 2024
The trim()
method of the String class in Java is used to remove leading and trailing whitespace from a string. The Unicode value of the space character is "\u0020". It does not remove any whitespace in the middle of the string. This method returns a new string with the whitespace removed and leaves the original string unchanged.
Example:
Below is an example that demonstrates the usage of the trim() method in Java.
Java
// Java program to demonstrate the
// usage of the trim() method
public class Trim {
public static void main(String[] args) {
String s1 = " Hello, Geeks ";
// Using trim() to remove
// leading and trailing whitespaces
String s2 = s1.trim();
System.out.println("Original String: '" + s1 + "'");
System.out.println("Trimmed String: '" + s2 + "'");
}
}
OutputOriginal String: ' Hello, Geeks '
Trimmed String: 'Hello, Geeks'
Syntax of trim() Method
String trimmedString = str.trim();
- Parameter: The trim() method accepts no parameters.
- Return Type: It returns a new string with leading and trailing whitespace removed. If no whitespace exists, it returns the original string unchanged.
Examples of trim() in Java
1. Using trim() to Remove Leading and Trailing Whitespaces
In this example, we will demonstrate how the trim()
method works by removing whitespaces from the beginning and end of the string.
Java
// Java program to demonstrate working
// of java string trim() method
import java.io.*;
class GFG {
public static void main (String[] args) {
// Three strings declared
String x = "geeks ";
String y = "for ";
String z = " geeks";
// Printing without trim function
System.out.println(x + y + z);
// Using trim function to get result
System.out.println(x.trim() + y.trim() + z.trim());
}
}
Outputgeeks for geeks
geeksforgeeks
Explanation: In this example, the trim() method is used to remove the starting and ending spaces from each string before concatenating them.
2. Use trim() that Does Not Modify the Original String
The trim() method returns the string rather than making changes to the original string.
Example:
Java
// Java program to demonstrate working
// of java string trim() method
public class Trim {
public static void main(String args[]) {
// String with leading and trailing spaces
String s = " Geeks for Geeks ";
// Printing String after removing the whitespaces
System.out.println(s.trim());
// Printing string to observe original string
System.out.println(s);
}
}
OutputGeeks for Geeks
Geeks for Geeks
Explanation: As we can see, the trim() method
returns a new string with the whitespaces removed, but the original string "s"
remains unchanged.
3. Use trim() to Compare the Original and Trimmed Strings
When we use the trim() method, we get the original and trimmed string. Both are different, in case we are removing the whitespaces from the original string.
Example:
Java
// Java program to demonstrate working
// of java string trim() method
public class Trim {
public static void main(String[] args) {
// String with leading and trailing spaces
String s1 = " Geeks For Geeks ";
// Before Trim() method
System.out.println("String 1: " + s1);
System.out.println("Length: " + s1.length());
// Applying trim() method on string s1
String s2 = s1.trim();
// After Trim() method
System.out.println("\nString 2: " + s2);
System.out.println("Length: " + s2.length());
// Comparing both the strings
if (s1 == s2) {
System.out.println("\nEqual");
} else {
System.out.println("\nNot Equal");
}
}
}
OutputString 1: Geeks For Geeks
Length: 21
String 2: Geeks For Geeks
Length: 15
Not Equal
Explanation: In this example, the trim()
method creates a new string without modifying the original string. As a result, comparing the original string s1
and the trimmed string s2
shows that they are not the same.
Similar Reads
JavaScript String trim() Method The trim() method is used to remove the whitespace from both sides of a string. It does not change the original string. It is used for trimming unnecessary spaces from strings or user input data. Note: This method does not modify the original string, it returns a new trimmed string.Syntaxstr.trim();
2 min read
Java String substring() Method In Java, the substring() method of the String class returns a substring from the given string. This method is most useful when you deal with text manipulation, parsing, or data extraction.This method either takes 1 parameter or 2 parameters, i.e., start and end value as arguments.In case the end par
2 min read
Java String split() Method The String split() method in Java is used to split a string into an array of substrings based on matches of the given regular expression or specified delimiter. This method returns a string array containing the required substring.Example: Here, we will split a String having multiple delimiters or re
6 min read
StringJoiner toString() method in Java The toString() of StringJoiner is used to convert StringJoiner to String. It returns the current value, consisting of the prefix, the values added so far separated by the delimiter, and the suffix, unless no elements have been added in which case, the prefix + suffix or the emptyValue characters are
2 min read
Java String length() Method String length() method in Java returns the length of the string. In this article, we will see how to find the length of a string using the String length() method.Example:Javaclass Geeks { public static void main(String[] args){ String s1 = "abcd"; System.out.println(s1.length()); String s2 = ""; Sys
2 min read
Java String charAt() Method String charAt() method in Java returns the character at the specified index in a string. The Index of the first character in a string is 0, the second character is 1, and so on. The index value should lie between 0 and length() - 1.If the index value is greater than or equal to the string length or
2 min read
Java String format() Method In Java, the String.format() method allows us to create a formatted string using a specified format string and arguments. We can concatenate the strings using this method, and at the same time, we can format the output with options such as width, alignment, decimal places, and more.Example: In the e
4 min read
JavaTuples toString() method The toString() method in org.javatuples is used to convert the values in TupleClass into a String. This method is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns a String value formed with the values in the TupleClassObject. Met
2 min read
Python String strip() Method strip() method in Python removes all leading and trailing whitespace by default. You can also specify a set of characters to remove from both ends. It returns a new string and does not modify the original.Let's take an example to remove whitespace from both ends of a string.Pythons = " GeeksforGeeks
2 min read
C# | TrimStart() and TrimEnd() Method Prerequisite: Trim() Method in C# In C#, TrimStart() & TrimEnd() are the string methods. TrimStart() method is used to remove the occurrences of a set of characters specified in an array from the starting of the current String object. TrimEnd() method is used to remove the occurrences of a set o
3 min read