转换kABPersonPhoneProperty以阿拉伯数字表示(convert kABPerson

2019-09-20 14:44发布

我是从手机通讯录读电话号码,有些号码在非阿拉伯数字保存有即(123456789)我怎么能这些数字转换为阿拉伯数字(123456789)

这就是我如何提取号码

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

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

Answer 1:

测试:

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


Answer 2:

我认为好的和简单的方法是使转换要转换为字符串的数字的方法,然后通过他们的方法,其中有两个不同的数字系统之间的映射的字典,然后返回结果

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


Answer 3:

用这个:

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

}



文章来源: convert kABPersonPhoneProperty to arabic numbers