我正在通过电话地址簿阅读电话号码,有些号码以非阿拉伯语号码保存在那里,即(123456789)如何将这些号码转换为阿拉伯数字(123456789)
这就是我如何提取数字
CFTypeRef phoneProperty = ABRecordCopyValue(record, kABPersonPhoneProperty);
NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);
现在手机包含的电话号码为NSString对象
答案 0 :(得分:4)
经过测试:
-(NSString*)arabicToWestern:(NSString)numericString {
NSMutableString *s = [NSMutableString stringWithString:numericString];
NSString *arabic = @"١٢٣٤٥٦٧٨٩";
NSString *western = @"123456789";
for (uint i = 0; i<arabic.length; i++) {
NSString *a = [arabic substringWithRange:NSMakeRange(i, 1)];
NSString *w = [western substringWithRange:NSMakeRange(i, 1)];
[s replaceOccurrencesOfString:a withString:w
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, s.length)];
}
return [NSString stringWithString:s];
}
答案 1 :(得分:0)
我认为好的和简单的方法是创建一个方法,将您想要转换的数字转换为字符串,然后在方法中传递它们,其中有一个字典在两个不同的数字系统之间进行映射,然后返回结果
private string convertDigits(string easternArabicnumbers)
{
string result = "";
var digitMapping = new Dictionary<string,string>;
digitMapping.Add("١","1");
digitMapping.Add("٢","2");
digitMapping.Add("٣","3");
digitMapping.Add("٤","4");
digitMapping.Add("٥","5");
digitMapping.Add("٦","6");
digitMapping.Add("٧","7");
digitMapping.Add("٨","8");
digitMapping.Add("٩","9");
digitMapping.Add("٠","0");
foreach(var digit in easternArabicnumbers)
{
if (digitMapping.ContainsKey(digit))
{
result = result + digitMapping[digit];
}
return result;
}
}
答案 2 :(得分:0)
使用它:
- (NSString *)ConvertNumToFarsi:(NSString *)EnglishNumString {
NSString *myString = [EnglishNumString stringByReplacingOccurrencesOfString:@"1" withString:@"۱"];
myString = [myString stringByReplacingOccurrencesOfString:@"2" withString:@"۲"];
myString =[myString stringByReplacingOccurrencesOfString:@"3" withString:@"۳"];
myString =[myString stringByReplacingOccurrencesOfString:@"4" withString:@"۴"];
myString =[myString stringByReplacingOccurrencesOfString:@"5" withString:@"۵"];
myString =[myString stringByReplacingOccurrencesOfString:@"6" withString:@"۶"];
myString =[myString stringByReplacingOccurrencesOfString:@"7" withString:@"۷"];
myString =[myString stringByReplacingOccurrencesOfString:@"8" withString:@"۸"];
myString =[myString stringByReplacingOccurrencesOfString:@"9" withString:@"۹"];
myString =[myString stringByReplacingOccurrencesOfString:@"0" withString:@"۰"];
return myString;
}