我已經爲它做了所有的代碼,但我有一些問題。並非所有無效字符都被刪除,但我無法拾取一個模式。我一直在嘗試很長時間才能找出造成這種情況的原因,最後我決定讓你們看看是否有人可以弄清楚。使代碼清理不需要的字符字符串
這裏是有效字符的字符陣列(所有其他字符將從字符串被刪除):
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