我在我的项目中使用StringBuilder。当我appen char一切正常,但当我尝试追加字符串“8(”或“)”或“”时,它保持为空“”。 示例(已编辑):
int length = number.length();
CharSequence sequence = number.subSequence(0,length);
StringBuilder stringBuilder = new StringBuilder(17);
for (int i = 0; i < length; i++){
if (Pattern.matches("^[\\d]$", String.valueOf(sequence.charAt(i)))){
stringBuilder.append(sequence.charAt(i));
}
}
sequence = stringBuilder.toString();
stringBuilder = new StringBuilder(17);
CharSequence s = "8 (";
if (sequence.charAt(0) == 8 || sequence.charAt(0) == 7 || sequence.length() == 0){
stringBuilder.append(s);
}
for (int i = 1; i < 11 && i < sequence.length(); i++){
stringBuilder.append(sequence.charAt(i));
if (i == 3){
stringBuilder.append(") ");
}
if (i == 6 || i == 8){
stringBuilder.append(' ');
}
}
number.clear();
number.clearSpans();
number.append(stringBuilder.toString());
答案 0 :(得分:1)
您的代码存在问题
if (sequence.charAt(0) == 8 || sequence.charAt(0) == 7 ||
你需要比较字符而不是整数
if (sequence.charAt(0) == '8' || etc