从String中移除空白字符的多种方式!?差别竟然这么大!
共 8872字,需浏览 18分钟
·
2020-09-14 21:59
trim() : 删除字符串开头和结尾的空格。 strip() : 删除字符串开头和结尾的空格。 stripLeading() : 只删除字符串开头的空格 stripTrailing() : 只删除字符串的结尾的空格 replace() : 用新字符替换所有目标字符 replaceAll() : 将所有匹配的字符替换为新字符。此方法将正则表达式作为输入,以标识需要替换的目标子字符串 replaceFirst() : 仅将目标子字符串的第一次出现的字符替换为新的字符串
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'
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'
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'
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'
\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'
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'