删除iOS中不需要的字符 [英] remove unwanted characters in iOS

查看:109
本文介绍了删除iOS中不需要的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在填写地址簿中的所有联系人。我必须向服务器发送没有国家代码和电话号码的电话号码。没有任何特殊字符和空间.i.e。纯电话号码。但是我的地址簿电话号码有不同的格式,比如这些

I am populating all contacts from address book. i have to send to server only phone numbers without country code & without any special characters & spaces .i.e. pure phone numbers. but my address book phone numbers are in different formats, like these

"+91 99-70-044099",
"*125#",
"+91 8605-681220",
"+919975806164",
9822771881,
02026697467,
"+**62*+91675#",
"+91 80-87177718",
"\U202a+91 81 49 753637\U202c",
"8308-412345",
"094-19-241898",
"*123*7#",
"+6596913434",
7898989876565657  

用于制作没有特殊字符的&空格,我做了以下代码

for making these without special characters & spaces , I did following code

NSError *error;
            NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
                                                                       error:&error];
            NSUInteger phoneValidates = [detector numberOfMatchesInString:phoneNumber
                                                                  options:0
                                                                    range:NSMakeRange(0, [phoneNumber length])];
          //  NSLog(@"res=%d",phoneValidates);

            if (phoneValidates)
            {

                NSLog(@"yes it is phone number %@",phoneNumber);
                NSString *str = [phoneNumber stringByReplacingOccurrencesOfString:@"+" withString:@"" ];
                NSLog(@"after=%@",str);
                str = [str stringByReplacingOccurrencesOfString:@" " withString:@"" ];
                NSLog(@"after=%@",str);
                str = [str stringByReplacingOccurrencesOfString:@"-" withString:@"" ];
                NSLog(@"after=%@",str);
                str = [str stringByReplacingOccurrencesOfString:@"\\U202a" withString:@"" ];
                NSLog(@"after=%@",str);

                str = [str stringByReplacingOccurrencesOfString:@"\\U202c" withString:@"" ];
                NSLog(@"after=%@",str);

                [arr_contact addObject:str];
            }

即使通过上面的代码,数组包含以下格式数字

even going through above code, array contains below format numbers

"\U202a918149753637\U202c", 

如何删除这些字符?如何只从这里得到数字。

一些数字是国家代码,然后如何删除国家代码,我的问题是
91是我的国家代码,但我也有一些数字开头91即实数,而不是国家代码。我也必须检查所有类型的国家代码。

How to remove these characters? How to get only number from this.
some numbers are with country code then how to remove country code, my problem is that 91 is my country code but also i have some number which starts with 91 i.e. real number, not a country code. also i have to check this for all types of country code.

推荐答案

仅获取数字:

NSMutableString *result = [NSMutableString stringWithCapacity:phoneNumber.length];

NSScanner *scanner = [NSScanner scannerWithString:phoneNumber];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

while ([scanner isAtEnd] == NO) 
{
  NSString *buffer;
  if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) 
   {
      [result appendString:buffer];
   } 
   else 
   {
      [scanner setScanLocation:([scanner scanLocation] + 1)];
   }
}

NSLog(@"%@", result);

这篇关于删除iOS中不需要的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆