convert kABPersonPhoneProperty to arabic numbers

冷暖自知 提交于 2019-12-12 10:22:17

问题


I'm reading phone numbers from the phone address book , some numbers are saved there in non arabic numbers i.e ( ١٢٣٤٥٦٧٨٩ ) how can i convert these numbers to arabic numbers ( 123456789 )

that's how i extract the numbers

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

now phone contains phone numbers as NSString objects


回答1:


Tested:

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



回答2:


I think the good and simple way is to make a method that converts the numbers you want to convert into strings, then pass them in the method where there is a dictionary that maps between the two different digit system and then return the results

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



回答3:


use this:

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

}



来源:https://stackoverflow.com/questions/11754545/convert-kabpersonphoneproperty-to-arabic-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!