2015-01-06 42 views
1

所以即時嘗試刪除電話號碼中的所有特殊字符只留下數字,不幸的是它不工作。我查了其他解決方案,但他們仍然沒有工作。即使在調試器中運行後,似乎.replaceAll()方法根本就沒有做任何事情。這裏是我下面的代碼:String.replaceAll()不工作在我的環島

if (c !=null) { 
      //populate database with first 100 texts of each contact 
      dbs.open(); 
      //comparator address, count to 100 
      String addressX = ""; 
      int count = 0; 
      c.moveToFirst(); 
      for (int i = 0; i < c.getCount(); i++) { 
       //get c address 
       String addressY = c.getString(c.getColumnIndex("address")); 
       addressY.replaceAll("[^0-9]+", "").trim(); 
       //one for the address, other to be able to sort threads by date !Find better solution! 
       if (!smsAddressList.contains(addressY)) { 
        //custom object so listview can be sorted by date desc 
        String date = c.getString(c.getColumnIndex("date")); 
        smsDateList.add(date); 
        smsAddressList.add(addressY); 
       } 
       //less than 100 texts, add to database, update comparator, add to count 
       if (count < 100) { 
        c.moveToPosition(i); 
         addText(c); 
         addressX = addressY; 
         count++; 
        } 
       //if more 100 texts, check to see if new address yet 
       else if (count>100) { 
        //if still same address, just add count for the hell of it 
        if (addressX.equals(addressY)) { 
         count++; 
        } 
        //if new address, add text, reset count 
        else { 
         addText(c); 
         addressX = addressY; 
         count = 1; 
        } 


       } 


      } 
     } 
+3

我喜歡你loooop的拼法) –

+1

@jan,它可能是由一個迴路中產生一個錯誤的錯誤:-) – paxdiablo

回答

4

String.replaceAll()返回修改過的字符串,它不會在原地修改字符串。所以,你需要這樣的東西:

addressY = addressY.replaceAll("[^0-9]+", "").trim(); 

我也相當肯定trim是多餘的,因爲在這裏你已經剝離出來與replaceAll空白。因此,你可以逃脫:

addressY = addressY.replaceAll("[^0-9]+", ""); 
+0

加上一個說這將返回修改後的字符串 –

2

字符串是不可改變的。 replaceAll只需返回具有所需修改的新字符串。

你需要做的:所以你需要reassing變量

addressY = addressY.replaceAll("[^0-9]+", "").trim(); 
+0

加上一個說簡單地返回新的字符串 –

2
addressY = addressY.replaceAll("[^0-9]+", "").trim(); 

replaceAll方法的客旅中一個新的String。

+1

只是返回新的字符串 –

1

確保您更改的字符串賦值給addressY

addressY = addressY.replaceAll("[^0-9]+", "").trim();