Home >Java >javaTutorial >How to use the trim() method of the String class to remove whitespace characters at both ends of a string
How to use the trim() method of the String class to remove whitespace characters at both ends of a string
In daily programming, sometimes we encounter situations where whitespace characters are included at both ends of a string. These whitespace characters may include Spaces, tabs, newlines, etc. If we want to remove these whitespace characters, we can use the trim() method in the String class. This article explains how to use the trim() method and provides code examples.
The String class is one of the most commonly used classes in Java. It provides many methods for manipulating strings. Among them, the trim() method is used to remove blank characters at both ends of the string and return a new string.
The following is a code example using the trim() method:
public class StringUtil { public static String trimString(String str) { if (str == null) { return null; } return str.trim(); } public static void main(String[] args) { String str = " Hello, World! "; String trimmedStr = trimString(str); System.out.println("原始字符串:" + str); System.out.println("去除空白字符后的字符串:" + trimmedStr); } }
In the above code, we define a StringUtil class, which contains a static method trimString(). This method receives a string parameter, then calls the trim() method of the String class to remove the blank characters at both ends of the string, and returns the string after removing the blank characters.
In the main() method, we declare a string containing whitespace characters " Hello, World! "
, and then call the trimString() method to process the string, and Save the processed results to the trimmedStr variable. Finally, we print out the original string and the string after removing whitespace characters through the System.out.println() method.
Run the above code, we can get the following output:
原始字符串: Hello, World! 去除空白字符后的字符串:Hello, World!
You can see that the blank characters at both ends of the original string have been removed, and a new character after removing the blank characters is obtained string.
It should be noted that the trim() method can only remove whitespace characters at both ends of the string, but cannot remove whitespace characters inside the string. If you need to remove all whitespace characters from a string, you can use regular expressions or other methods to achieve this.
Summary:
This article introduces how to use the trim() method of the String class to remove blank characters at both ends of the string. By calling the trim() method, we can easily remove the blank characters at both ends of the string and get a new string. I hope this article will help you with string processing in daily programming.
The above is the detailed content of How to use the trim() method of the String class to remove whitespace characters at both ends of a string. For more information, please follow other related articles on the PHP Chinese website!