首页 文章

删除iOS中不需要的字符

提问于
浏览
0

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

"+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

为了使这些没有特殊字符和空格,我做了以下代码

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

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

"\U202a918149753637\U202c",

如何删除这些字符?如何从中获取数字 .
一些数字是国家代码然后如何删除国家代码,我的问题是91是我的国家代码,但我有一些数字,从91开始,即实数,而不是国家代码 . 我还必须检查所有类型的国家代码 .

4 回答

  • 5

    只获得数字:

    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);
    
  • 1
    NSString * strippedNumber =
     [mobileNumber stringByReplacingOccurrencesOfString:@"[^0-9]"
                                             withString:@""
                                                options:NSRegularExpressionSearch
                                                  range:NSMakeRange(0, [mobileNumber length])];
    
  • 0

    \ U202a,\ U202b,\ U202c,\ U202d是控制字符 . 它们在NSCharacterSet中定义 . 所以这是从给定字符串中删除控制字符的一种方法..

    NSCharacterSet *cc = [NSCharacterSet controlCharacterSet];
    NSString *cs = [[searchString componentsSeparatedByCharactersInSet:cc]
                    componentsJoinedByString:@""];
    

    控制字符属于Cc和Cf类别 . http://www.fileformat.info/info/unicode/category/index.htm

  • 4

    试试以下代码:

    我是这样做的:

    NSMutableString *strippedString = [NSMutableString stringWithCapacity:10];
    

    我最终得到一个空字符串,但代码不会崩溃 .

    我不得不求助于旧的C:

    for (int i=0; i<[phoneNumber length]; i++) 
     {
         if (isdigit([phoneNumber characterAtIndex:i]))
         {
             [strippedString appendFormat:@"%c",[phoneNumber characterAtIndex:i]];
         }
     }
    

相关问题