自己写一个方法,使用split()去除角空格以及全角空格!~ split()返回是String[],后再组合成一个String
public String[] split(String regex)根据给定正则表达式的匹配拆分此字符串。
该方法的作用就像是使用给定的表达式和限制参数 0 来调用两参数 split 方法。因此,所得数组中不包括结尾空字符串。
例如,字符串 "boo:and:foo" 使用这些表达式可生成以下结果:
Regex 结果
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
参数:
regex - 定界正则表达式
返回:
字符串数组,它是根据给定正则表达式的匹配拆分此字符串确定的,,
根据你的要求重新编写了以个程式,测试OK,
代码如下:
class StrTest{
static String stringTest(String s){
int j=0,k=0,i=0;
char[] stra=new char[s.length()];
s.getChars(0,s.length(),stra,0);
for(i=0;i<s.length();i++){
if(stra[i]==' '||stra[i]==' '){
j=i+1;
}
else
{
break;
}
}
for(i=s.length()-1;i+1>0;i--){
if(stra[i]==' '||stra[i]==' '){
k=i;
}
else{
break;
}
}
String strb=new String(stra,j,k-j);
return strb;
}
public static void main(String[] args){
String str=new String(" a test test ");
System.out.println(stringTest(str));
}
}
你可以更改 String str=new String(" a test test "); 来测试
温馨提示:答案为网友推荐,仅供参考