我已经为它做了所有的代码,但我有一些问题。并非所有无效字符都被删除,但我无法拾取一个模式。我一直在尝试很长时间才能找出造成这种情况的原因,最后我决定让你们看看是否有人可以弄清楚。使代码清理不需要的字符字符串
这里是有效字符的字符阵列(所有其他字符将从字符串被删除):
static char[] validCharsUsername ={'Q','q','W','w','E','e','R','r','T','t','Y','y','U','u','I','i','O','o','P','p','A','a','S','s','D','d','F','f','G','g','H','h','J','j','K','k','L','l','Z','z','X','x','C','c','V','v','B','b','N','n','M','m','1','2','3','4','5','6','7','8','9','0','_','-'};
下面是代码。 (this.validChars
是指的数组):
public String cleanString(String text){
StringBuilder sb = new StringBuilder(text);
for(int i = 0;i < sb.length() - 1;i++){
char character = sb.charAt(i);
int index = 0;
char indexChar = this.validChars[0];
boolean valid = false;
while(index < this.validChars.length - 1){
index++;
indexChar = this.validChars[index];
if(character == indexChar){
valid = true;
index = this.validChars.length;
}
}
if(!valid){
if(character == ' '){
sb.deleteCharAt(i);
sb.insert(i, '_');
}else{
sb.deleteCharAt(i);
}
i = 0;
}
}
return sb.toString();
}
什么是exmaple输入输出不起作用? – 2014-08-27 19:36:35
@Pshemo改变了它,我的意思是有效的,对不起。 – vedi0boy 2014-08-27 19:58:05