0

我正在从电话通讯簿中读取电话号码,一些号码以非阿拉伯数字保存在那里,即(١٢٣٤٥٦٧٨٩)我如何将这些数字转换为阿拉伯数字(123456789)

这就是我提取数字的方式

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

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

4

3 回答 3

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];
}
于 2012-08-01T08:13:46.067 回答
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;
        }
    }
于 2012-08-01T07:50:14.157 回答
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;

}

于 2013-09-14T11:46:25.017 回答