Trim Method in String Java
What is a String?
Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language.
“String” is a Java platform class that allows you to construct and handle strings
Trim Method:-
There are numerous methods available in Java. One of these, the trim method in Java, will be the subject of this discussion. The Java trim method is a built-in function for removing leading and trailing spaces from a string. Let's take a closer look at the subject.
In Java, the trim() function can also be used to trim characters.
But it is also Noted that the trim() method does not remove spaces in the middle.
Syntax:
public String trim()
Return Type:
Trim() returns a String as its return type. The excluded string is returned with no leading or trailing spaces.
Parameters:
The trim method does not have any parameters.
How does the Trim Method work?
'u0020' is the Unicode value for the space character. Trim in Java examines whether the Unicode value 'u0020' is present before and after the given string when the code is processed in Java. If the Unicode value 'u0020' exists, the trim method in Java removes the spaces and produces a new string.
Now let’s take a view on some Examples:
// Java string trim() method
class Gfg {
public static void main(String args[])
{
// trims the leading spaces
String s = " welcome to my world ";
System.out.println(s.trim());
// trims the leading spaces there also
s = "I want to become a programmer ";
System.out.println(s.trim());
}
}
Output:
Welcome to my world
I want to become a programmer
Points to remember:
- In Java, the trim() method does not eliminate spaces in the center of a string.
- In Java, the trim() method creates a new String object without any leading or trailing spaces.
- This approach does not change the original string.
Example :
class Main {
public static void main(String[] args) {
String str = "Hey my name is John ";
String newStr = str.trim();
System.out.println(newStr);
}
}
Output:
My name is John
Explanation:
The string "My name is John" is given to us in the preceding example. There is one leading space and four trailing spaces in the supplied string. Now, when the code is compiled, the trim function in Java will meet 1 'u0020' character at the start and 4 'u0020' characters at the conclusion. As a result, it would remove all of these words and return a new string that reads "My name is John."
Example[removing all whitespace characters]:
To remove all whitespace characters from a string, we can use the replaceAll() function in Java.
The example below demonstrates how to remove all whitespace characters from a string.
class Main {
public static void main(String[] args) {
String str = "My name is John ";
String newStr= str1.replaceAll("\\s", "");
System.out.println(newStr);
}
}
Output:
MynameisJohn
Explanation:
We're given the string "My name is John" in this example, and we're meant to remove all whitespace characters. The str1.replaceAll() function is used to do this.
The first parameter is a whitespace regex, and the second parameter is an empty string. As a result, the replaceAll method will replace all whitespace characters in the string with an empty string, yielding a new string containing the text "MynameisJohn."
Conclusion:
In Java, the trim() function removes excess leading and trailing spaces from a string.
It accepts no arguments and returns a new string
In Java, the trim() method does not remove any middle spaces.
'u0020' is the Unicode value for the space character.
The trim technique looks for the space's Unicode value and removes it.
The replaceAll() method can be used to remove all whitespace characters from a string.
Example: Program to show the working of java string trim() method
class GFG {
public static void main (String[] args) {
String s1 = " hello world ";
// Before Trim method
System.out.println("Before Trim() - ");
System.out.println("String - "+s1);
System.out.println("Length - "+s1.length());
// applying trim method string s1
s1=s1.trim();
// After Trim method
System.out.println("\nAfter Trim() - ");
System.out.println("String - "+s1);
System.out.println("Length - "+s1.length());
}
}
Output:
Before Trim -
String - hello world
Length - 17
After Trim -
String – hello world