In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the ways to delete spaces in String". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the ways to delete spaces in String?"
Different ways to remove spaces from a string in java
First, let's take a look at how many methods there are to remove spaces from String. Based on experience, the author summarizes the following seven (JDK native methods that do not include similar methods in third-party tool class libraries):
Trim (): removes spaces at the beginning and end of a string.
Strip (): removes spaces at the beginning and end of a string.
StripLeading (): delete only the spaces at the beginning of the string
StripTrailing (): delete only the spaces at the end of the string
Replace (): replaces all target characters with new characters
ReplaceAll (): replaces all matching characters with new characters. This method takes a regular expression as input to identify the target substring that needs to be replaced
ReplaceFirst (): replaces only the first occurrence of the target substring with the new string
The most important thing to note is that the String object is immutable in Java, which means we can't modify the string, so all the above methods get a new string.
Next, we learn the usage of the above methods and understand their characteristics.
PS: all of the code in this article is executed using the online run tool, because my test machine does not have Java 11 installed and the Unicode characters are incomplete. If you also want to experiment, it is recommended to use online tools and select the corresponding JDK.
Trim
Trim () is the most commonly used method for Java developers to remove spaces at the beginning and end of a string. Its usage is also relatively simple:
Public class StringTest {public static void main (String [] args) {String stringWithSpace = "Hollis Is A Java Coder"; StringTest.trimTest (stringWithSpace);} private static void trimTest (String stringWithSpace) {System.out.println ("Before trim:\" + stringWithSpace + "\"); String stringAfterTrim = stringWithSpace.trim (); System.out.println ("After trim:\" + stringAfterTrim + "\") }}
Output result:
Before trim: 'Hollis Is A Java Coder' After trim:' Hollis Is A Java Coder'
As above, after using trim, the space content at the beginning and end of the original string is removed.
But have you ever thought about what the blank content removed by the trim method contains? Are there any other characters besides spaces?
In fact, the white space character removed by trim refers to any character whose ASCII value is less than or equal to 32 ('Utility 0020'):
It contains characters such as spaces, line breaks, backspaces, etc.
Strip ()
I don't know if you've noticed that in the release of Java 11, a new strip () method has been added to remove leading and trailing spaces from strings.
Why add a new strip when you already have a trim method?
This is actually because the trim method can only remove characters with an ASCII value less than or equal to 32, but according to the Unicode standard, there are many other white space characters in addition to the characters in ASCII.
And to recognize these white space characters, a new isWhitespace (int) method has been added to the Character class starting with Java 1.5. This method uses unicode to identify space characters. You can learn more about unicode space characters at jkorpela.fi/chars/spaces.html.
The new strip method in Java 11 uses this Character.isWhitespace (int) method to determine whether it is white space characters and delete them:
Let's look at an example of using strip:
Public class StringTest {public static void main (String args []) {String stringWithSpace ='\ u2001'+ "Hollis Is A Java Coder" +'\ u2001'; System.out.println ("'" +'\ u2001' + "'is space:" + Character.isWhitespace ('\ u2001')); StringTest.stripTest (stringWithSpace) } private static void stripTest (String stringWithSpace) {System.out.println ("Before strip:\'" + stringWithSpace + "\'"); String stringAfterTrim = stringWithSpace.strip (); System.out.println ("After strip:\'" + stringAfterTrim + "\'");}}
We added a special character\ u2001 before and after the string, which is not in ASCII. It is judged by Character.isWhitespace to be a blank character. Then use strip for processing, and the output is as follows:
' ' is space: trueBefore strip: 'Hollis Is A Java Coder' After strip:' Hollis Is A Java Coder'
Therefore, the strip method in Java 11 is more powerful than the trim method, it can remove a lot of white space characters that are not in the ASCII, and the judgment method is through the Character.isWhitespace method.
StripLeading () and stripTrailing ()
The stripLeading () and stripTrailing () methods are also added in Java 11. The function is to delete the space at the beginning of the string and the space at the end of the string.
Similar to the strip method, stripLeading and stripTrailing also use Character.isWhitespace (int) to identify white space characters. The usage is also similar to strip:
Public class StringTest {public static void main (String args []) {String stringWithSpace ='\ u2001'+ "Hollis Is A Java Coder" +'\ u2001'; System.out.println ("'" +'\ u2001' + "'is space:" + Character.isWhitespace ('\ u2001')); StringTest.stripLeadingTest (stringWithSpace); StringTest.stripTrailingTest (stringWithSpace) } private static void stripLeadingTest (String stringWithSpace) {System.out.println ("Before stripLeading:\'" + stringWithSpace + "\'"); String stringAfterTrim = stringWithSpace.stripLeading (); System.out.println ("After stripLeading:\" + stringAfterTrim + "\");} private static void stripTrailingTest (String stringWithSpace) {System.out.println ("Before stripTrailing:\"+ stringWithSpace +"\ ") String stringAfterTrim = stringWithSpace.stripTrailing (); System.out.println ("After stripTrailing:\" + stringAfterTrim + "\");}}
Output result:
' ' is space: trueBefore stripLeading: 'Hollis Is A Java Coder' After stripLeading:' Hollis Is A Java Coder' Before stripTrailing: 'Hollis Is A Java Coder' After stripTrailing:' Hollis Is A Java Coder'replace
In addition to using trim and strip, another way to remove white space characters from a string is to use the replace method to replace the white space characters.
Replace is added from java 1.5, and each target substring can be replaced with a specified string.
This method replaces all matching target elements as follows:
Public class StringTest {public static void main (String args []) {String stringWithSpace = "Hollis Is A Java Coder"; StringTest.replaceTest (stringWithSpace);} private static void replaceTest (String stringWithSpace) {System.out.println ("Before replace:\"+ stringWithSpace +"\ "); String stringAfterTrim = stringWithSpace.replace (",") System.out.println ("After replace:\" + stringAfterTrim + "\");}}
Results:
Before replace: 'HollisIsAJavaCoder' After replace:' HollisIsAJavaCoder'
As you can see, using the replace method above, you can replace all white space characters in the string. In particular, the replace method, like the trim method, can only replace white space characters in ASCII.
ReplaceAll
ReplaceAll is one of the most powerful string manipulation methods added in Java 1.4. We can use this method for many purposes.
Using the replaceAll () method, we can use regular expressions to identify the target character content that needs to be replaced. Using regular expressions, you can achieve many functions, such as deleting all spaces, deleting opening spaces, deleting ending spaces, and so on.
We just need to create the correct regular expression with the correct replacement parameters. Some examples of regular expressions are as follows:
\ s + all white space characters ^\ s + all white space characters at the beginning of the string\ all white space characters at the end of the string
Note that to add / we must use escape characters in java, so for\ s + we must use\\ s +
Public class StringTest {public static void main (String args []) {String stringWithSpace = "Hollis Is A Java Coder"; StringTest.replaceAllTest (stringWithSpace, ""); StringTest.replaceAllTest (stringWithSpace, "\\ s +"); StringTest.replaceAllTest (stringWithSpace, "^\\ s +"); StringTest.replaceAllTest (stringWithSpace, "\\ s $") } private static void replaceAllTest (String stringWithSpace,String regex) {System.out.println ("Before replaceAll with'" + regex + ":\"+ stringWithSpace +"\ "); String stringAfterTrim = stringWithSpace.replaceAll (regex,"); System.out.println (" After replaceAll with'"+ regex +"':\ "" + stringAfterTrim + "\");}}
Results:
Before replaceAll with': 'HollisIsAJavaCoder' After replaceAll with': 'HollisIsAJavaCoder'Before replaceAll with'\ sboxes: 'HollisIsAJavaCoder' After replaceAll with'\ sboxes: 'HollisIsAJavaCoder'Before replaceAll with' ^\ sboxes: 'HollisIsAJavaCoder' After replaceAll with' ^\ sboxes: 'HollisIsAJavaCoder'Before replaceAll with'\ sboxes: 'Hollis Is A Java Coder' After replaceAll with'\ s thanks thanks: 'Hollis Is A Java Coder'
As we can see, replaceAll () can be a very powerful method if used with the appropriate regular expression.
ReplaceFirst
The replaceFirst method, also added in java 1.4, simply replaces the first match of a given regular expression with a replacement string.
This method is useful if you only need to replace what happens the first time. For example, if we only need to delete leading spaces, we can use\\ s + or ^\\ s +.
We can also use this method to remove trailing spaces by using the\\ regular $regular expression. Because this expression will only match the last space of the line. So the last space is considered to be the first match for this method.
Let's give an example of removing leading and trailing spaces from a string.
Public class StringTest {public static void main (String args []) {String stringWithSpace = "Hollis Is A Java Coder"; StringTest.replaceFirstTest (stringWithSpace, ""); StringTest.replaceFirstTest (stringWithSpace, "\\ s +"); StringTest.replaceFirstTest (stringWithSpace, "^\\ s +"); StringTest.replaceFirstTest (stringWithSpace, "\\ s $") } private static void replaceFirstTest (String stringWithSpace,String regex) {System.out.println ("Before replaceFirst with'" + regex + ":\"+ stringWithSpace +"\ "); String stringAfterTrim = stringWithSpace.replaceFirst (regex,"); System.out.println (" After replaceFirst with'"+ regex +"':\ "" + stringAfterTrim + "\");}}
Results:
Before replaceFirst with'': 'Hollis Is A Java Coder' After replaceFirst with': 'Hollis Is A Java Coder' Before replaceFirst with'\ s stories: 'Hollis Is A Java Coder' After replaceFirst with'\ s stories: 'Hollis Is A Java Coder' Before replaceFirst with'^\ s stories: 'Hollis Is A Java Coder' After replaceFirst with'^\ s stories: 'Hollis Is A Java Coder' Before replaceFirst with'\ s stories: 'Hollis Is A Java Coder' After replaceFirst with'\ s stories: 'Hollis Is A Java Coder' is here I believe that you have a deeper understanding of "what are the ways to delete spaces in String?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.