Home >
Java >
javaTutorial >
What are the ways to remove spaces from String strings in java?
What are the ways to remove spaces from String strings in java?
WBOY
Release: 2023-05-17 23:31:55
forward
1059 people have browsed it
There are many different ways to remove spaces from strings in Java, such as trim, replaceAll, etc. However, some new features have been added in JDK 11, such as strip, stripLeading, stripTrailing, etc.
How many methods are there to remove spaces from String? The following introduces the native methods of JDK, excluding similar methods in third-party tool libraries
trim() : Remove spaces at the beginning and end of the string.
strip() : Remove spaces at the beginning and end of the string.
stripLeading() : Only remove spaces at the beginning of the string
stripTrailing() : Remove only the spaces at the end of the string
replace() : Replace all target characters with new characters
replaceAll() : Replace all matching characters with new characters. This method takes as input a regular expression to identify the target substring that needs to be replaced
replaceFirst() : Replace only the first time of the target substring Occurring characters are replaced with new string
The most important thing to note is that in Java String object is immutable which means we cannot modify the string so All the above methods give us a new string.
trim()
trim() is the most commonly used method by Java developers to remove spaces at the beginning and end of a string
public class StringTest {
public static void main(String[] args) {
String stringWithSpace = " Hello word java ";
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 + "\'");
}
}
Copy after login
Output results
Before trim : ' Hello word java ' After trim : 'Hello word java'
By using the trim method, the beginning and end of the original string The spaces have been removed. In fact, the whitespace characters removed by trim refer to any character with an ASCII value less than or equal to 32 (’ U 0020 '):
strip()
In the release of JDK 11, a new strip() method was added to remove leading and trailing spaces from strings. The
trim method can only remove characters whose ASCII value is less than or equal to 32, but according to the Unicode standard, in addition to characters in ASCII, there are still many other whitespace characters.
And in order to recognize these space characters, starting from Java 1.5, a new isWhitespace(int) method has been added to the Character class. This method uses unicode to identify space characters.
The strip method newly added in Java 11 uses the Character.isWhitespace(int) method to determine whether they are whitespace characters and delete them:
' is space : true Before strip : 'Hello word java ' After strip : 'Hello word java'
strip() method in Java 11 is faster than trim()# The ## method is more powerful. It can remove many whitespace characters that are not in ASCII. The way to judge is through the Character.isWhitespace() method.
The difference between trim() and strip() methods
trim
strip
Java 1 Introduced
Java 11 Introduced
Use ASCII
Use Unicode values
Remove the beginning and trailing whitespace characters
Delete leading and trailing whitespace characters
Delete characters whose ASCII value is less than or equal to ’U 0020’ or ’32’
Remove all whitespace characters according to unicode
' ' is space : true 删除开头的空白字符 Before stripLeading : ' Hello word java ' After stripLeading : 'Hello word java ' 删除结尾的空白字符 Before stripTrailing : ' Hello word java ' After stripTrailing : ' Hello word java'
public class StringTest {
public static void main(String args[]) {
String stringWithSpace =" Hello word java ";
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 + "\'");
}
}
Copy after login
Before replaceAll with ' ': ' Hello word java ' After replaceAll with ' ': 'Hellowordjava' Before replaceAll with '\s+': ' Hello word java ' After replaceAll with '\s+': 'Hellowordjava' Before replaceAll with '^\s+': ' Hello word java ' After replaceAll with '^\s+': 'Hello word java ' Before replaceAll with '\s+$': ' Hello word java ' After replaceAll with '\s+$': ' Hello word java'
public class StringTest {
public static void main(String args[]) {
String stringWithSpace =" Hello word java ";
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 + "\'");
}
}
Copy after login
结果:
Before replaceFirst with ' ': ' Hello word java ' After replaceFirst with ' ': ' Hello word java ' Before replaceFirst with '\s+': ' Hello word java ' After replaceFirst with '\s+': 'Hello word java ' Before replaceFirst with '^\s+': ' Hello word java ' After replaceFirst with '^\s+': 'Hello word java ' Before replaceFirst with '\s+$': ' Hello word java ' After replaceFirst with '\s+$': ' Hello word java'
The above is the detailed content of What are the ways to remove spaces from String strings in java?. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn