java字符串。replaceAll()在我的looop中不工作
因此,我试图删除电话号码中的所有特殊字符,只留下数字,不幸的是,它不起作用。我已经找到了其他解决方案,但它们仍然不起作用。即使在调试器中运行了它,它看起来仍然是。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;
}
}
}
}
# 1 楼答案
确保将更改的字符串分配给
addressY
# 2 楼答案
replaceAll
方法创建一个新字符串,因此需要重新分配变量# 3 楼答案
String.replaceAll()
返回修改后的字符串,它不会在适当的位置修改字符串。所以你需要一些类似的东西:我也非常确定
trim
在这里是多余的,因为您已经用replaceAll
去除了空白。因此,您可以逃脱:# 4 楼答案
字符串是不可变的
replaceAll
只返回带有所需修改的新字符串您需要执行以下操作: