2017-01-12 78 views
-2

我在我的項目中使用了StringBuilder。當我appen char時,一切正常,但當我嘗試追加字符串「8(」或「)」或「」時,它保持空白「」。 例(編者):Java StringBuilder不會附加字符串

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()); 
+5

我們展示足夠的代碼,以實際證明這個問題的時候跑。 – user2357112

+1

你是如何得出那個sb =「」的? '的ToString()'? –

+0

無法複製 –

回答

1

與您的代碼的問題是

if (sequence.charAt(0) == 8 || sequence.charAt(0) == 7 || 

你需要比較字符不是整數

if (sequence.charAt(0) == '8' || etc