空字符?你说的是空格??
用正则表达式吧
import java.util.regex.*;
import java.util.*;
class testReg{
public static void main(String args[]) {
String str;
String parentString="here is a test hehe !";
String regexString="\\S";
Pattern pattern = Pattern.compile(regexString);
Matcher m=pattern.matcher(parentString);
while(m.find()){
str=m.group();
System.out.print(str);}
}
}
用正则表达式吧
import java.util.regex.*;
import java.util.*;
class testReg{
public static void main(String args[]) {
String str;
String parentString="here is a test hehe !";
String regexString="\\S";
Pattern pattern = Pattern.compile(regexString);
Matcher m=pattern.matcher(parentString);
while(m.find()){
str=m.group();
System.out.print(str);}
}
}
String s = "This Is A Test String";
char[] cc = s.toCharArray() ;
for(int i = 0; i<s.length() ; i++){
if ( cc[i] == (char)32){
n = n + 1;
for(int j = i; j<(s.length()-n) ; j++)
cc[j] = cc[j+1];
}
}
for(int i = 0 ; i < n ; i++)
cc[s.length() - i - 1 ] = ' ';
s = String.valueOf(cc) ;
System.out.println(s.trim()) ;输出为:ThisIsATestString
你的方法太繁了,实现你的功能一条语句就可以:)
s.replaceAll("\u0020","");
原来想把空格字符替换成空字符就行了,
我也以为空字符是'\0'和'\u0000',但它也要占位的
用 Congar(游吟诗人) 的方法就行了。多谢!