从String中移除空白字符的多种方式!?差别竟然这么大!
在java中从字符串中删除空格的不同方法
trim() : 删除字符串开头和结尾的空格。 strip() : 删除字符串开头和结尾的空格。 stripLeading() : 只删除字符串开头的空格 stripTrailing() : 只删除字符串的结尾的空格 replace() : 用新字符替换所有目标字符 replaceAll() : 将所有匹配的字符替换为新字符。此方法将正则表达式作为输入,以标识需要替换的目标子字符串 replaceFirst() : 仅将目标子字符串的第一次出现的字符替换为新的字符串
trim
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 + "\'");
}
}
Before trim : ' Hollis Is A Java Coder '
After trim : 'Hollis Is A Java Coder'
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 + "\'");
}
}
' ' is space : true
Before strip : ' Hollis Is A Java Coder '
After strip : 'Hollis Is A Java Coder'
trim 和 strip 方法的区别
stripLeading() 和 stripTrailing()
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 + "\'");
}
}
' ' is space : true
Before 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
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 + "\'");
}
}
Before replace : ' Hollis Is A Java Coder '
After replace : 'HollisIsAJavaCoder'
replaceAll
\s+ 所有的空白字符
^\s+ 字符串开头的所有空白字符
\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 + "\'");
}
}
Before replaceAll with ' ': ' Hollis Is A Java Coder '
After replaceAll with ' ': 'HollisIsAJavaCoder'
Before replaceAll with '\s+': ' Hollis Is A Java Coder '
After replaceAll with '\s+': 'HollisIsAJavaCoder'
Before replaceAll with '^\s+': ' Hollis Is A Java Coder '
After replaceAll with '^\s+': 'Hollis Is A Java Coder '
Before replaceAll with '\s+$': ' Hollis Is A Java Coder '
After replaceAll with '\s+$': ' Hollis Is A Java Coder'
replaceFirst
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 + "\'");
}
}
Before replaceFirst with ' ': ' Hollis Is A Java Coder '
After replaceFirst with ' ': ' Hollis Is A Java Coder '
Before replaceFirst with '\s+': ' Hollis Is A Java Coder '
After replaceFirst with '\s+': 'Hollis Is A Java Coder '
Before replaceFirst with '^\s+': ' Hollis Is A Java Coder '
After replaceFirst with '^\s+': 'Hollis Is A Java Coder '
Before replaceFirst with '\s+$': ' Hollis Is A Java Coder '
After replaceFirst with '\s+$': ' Hollis Is A Java Coder'
总结
往期推荐
为什么程序员都不喜欢使用 switch ,而是大量的 if……else if ?
为啥不能用uuid做MySQL的主键!?
为什么 Java 中“1000==1000”为false,而”100==100“为true?
直面Java第329期:哪个命令可以监控虚拟机各种运行状态信息?
深入并发第013期:拓展synchronized——锁优化
如果你喜欢本文,
请长按二维码,关注 Hollis.
转发至朋友圈,是对我最大的支持。