Java String.replaceAll()在我的looop中不工作
因此,我试图删除电话号码中的所有特殊字符,只留下数字,不幸的是,它不起作用。我已经找到了其他解决方案,但它们仍然不起作用。即使在调试器中运行它,似乎.replaceAll方法也没有做任何事情。下面是我的代码:Java String.replaceAll()在我的looop中不工作,java,android,string,replaceall,Java,Android,String,Replaceall,因此,我试图删除电话号码中的所有特殊字符,只留下数字,不幸的是,它不起作用。我已经找到了其他解决方案,但它们仍然不起作用。即使在调试器中运行它,似乎.replaceAll方法也没有做任何事情。下面是我的代码: if (c !=null) { //populate database with first 100 texts of each contact dbs.open(); //comparator address, co
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;
}
}
}
}
字符串是不可变的。replaceAll只返回带有所需修改的新字符串 您需要执行以下操作:
addressY = addressY.replaceAll("[^0-9]+", "").trim();
字符串是不可变的。replaceAll只返回带有所需修改的新字符串 您需要执行以下操作:
addressY = addressY.replaceAll("[^0-9]+", "").trim();
replaceAll方法创建一个新字符串,因此需要重新分配变量
replaceAll方法创建一个新字符串,因此您需要重新分配变量。String.replaceAll返回修改后的字符串,但不会就地修改字符串。所以你需要一些类似的东西:
addressY = addressY.replaceAll("[^0-9]+", "").trim();
我也很确定修剪在这里是多余的,因为你们已经用replaceAll去掉了空白。因此,您可以逃脱:
addressY = addressY.replaceAll("[^0-9]+", "");
replaceAll返回修改后的字符串,它不会就地修改字符串。所以你需要一些类似的东西:
addressY = addressY.replaceAll("[^0-9]+", "").trim();
我也很确定修剪在这里是多余的,因为你们已经用replaceAll去掉了空白。因此,您可以逃脱:
addressY = addressY.replaceAll("[^0-9]+", "");
确保将更改的字符串分配给addressY
确保将更改的字符串分配给addressY
加一表示返回修改后的字符串加一表示返回修改后的字符串加一表示简单返回新字符串加一表示简单返回新字符串简单返回新字符串简单返回新字符串简单返回新字符串我喜欢你的looop拼写@jan,它可能是由一个带有off by one错误的循环生成的:-我喜欢你对loooop的拼写@jan,它可能是由一个带有off by one错误的循环生成的:-