gpt4 book ai didi

java - 从电话号码中删除国家代码

转载 作者:行者123 更新时间:2023-11-29 14:13:30 25 4
gpt4 key购买 nike

我遇到了一些联系人包含国家/地区代码而其他联系人不包含的问题。

我有用于获取保存在列表中的联系电话的代码。该列表如下所示:

[9999999999, 91-8888888888, 91-0000000000, 1111111111]

现在,我想从那些有国家代码的号码中删除国家代码。我想不出一种方法来检查这个。

这是我获取联系人的代码:

ContentResolver cr = getApplicationContext().getContentResolver(); //Activity/Application android.content.Context
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
if(cursor.moveToFirst())
{

do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
while (pCur.moveToNext())
{
String contactName = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY));
String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

allContacts.add(contactName);
contactNumber = contactNumber.replaceAll("\\s+","");
allnumbers.add(contactNumber);

break;
}
pCur.close();
}

} while (cursor.moveToNext()) ;

}
}

此处,allnumbers 包含号码(有些号码带有国家代码,有些则没有)。我怎样才能从拥有国家代码的人那里删除国家代码?

任何帮助或关键字搜索将不胜感激,谢谢。

最佳答案

如果您想删除第一次出现的包含“91-”的字符串,您可以简单地使用 replaceFirst() 方法。所以你的代码最终会像这样:

contactNumber.replaceAll("\\s+","").replaceFirst("91-","");

如果你想检查一个字符串是否以 '91-' 开头,你可以使用这个:

if (contactNumber.startsWith("91-")) {

}

关于java - 从电话号码中删除国家代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com