将kABPersonPhoneProperty转换为阿拉伯数字

我正在阅读手机通讯录中的电话号码,一些数字保存在非阿拉伯数字,即(123456789)如何将这些数字转换为阿拉伯数字(123456789)

这就是我如何提取数字

CFTypeRef phoneProperty = ABRecordCopyValue(record, kABPersonPhoneProperty); NSArray *phones = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty); 

现在手机包含电话号码作为NSString对象

testing:

 -(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]; } 

我认为最简单的方法是创build一个方法,将要转换的数字转换为string,然后在有两个不同数字系统之间映射的字典的方法中传递它们,然后返回结果

  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; } } 

用这个:

 - (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; 

}