2015-01-06 15 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();