【问题标题】:Remove particular String from NSArray not Object从 NSArray 中删除特定字符串而不是对象
【发布时间】:2016-02-02 05:22:37
【问题描述】:

我是 IOS 开发的新手。我在 NSArray 中获取电话号码,这是这样的。

9429564999,
9428889239,
7878817072,
"+919408524477",
9909951666,
9879824567,
"+91 8469-727523",
"94-28-037780",
"+918460814614",
55246,
"8866-030880",
"95-37-223347",
"+919574777070",
"+917405750526",

现在我想从 NSArray 列表中删除 -+91 而不是整个对象。如何做到这一点请有人帮忙

【问题讨论】:

  • 什么完整的对象?澄清你的问题

标签: ios objective-c iphone nsarray xcode7


【解决方案1】:
for (NSString *number in arrayName) {

number = [number stringByReplacingOccurrencesOfString:@"+91"
                                     withString:@""] mutableCopy];


number = [number stringByReplacingOccurrencesOfString:@"-"
                                     withString:@""] mutableCopy];
[arrayNameNew addObject:number];

}

试试这样 :)

快乐编码。

【讨论】:

    【解决方案2】:
    for (NSString *phoneNum in phoneNumArray) {
        phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"+91" withString:@""];
        phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"-" withString:@""];    
    }
    

    【讨论】:

      【解决方案3】:
      NSArray *phoneNoList = [[NSArray alloc]initWithObjects:@"9429564999",@"9428889239",@"7878817072",@"+919408524477",@"9909951666",@"9879824567", @"+91 8469-727523",@"94-28-037780",@"+918460814614",@"55246",@"8866-030880",@"95-37-223347",@"+919574777070", @"+917405750526", nil];
          NSMutableArray *noArr = [[NSMutableArray alloc]init];
          for (NSString *no in phoneNoList) {
              NSString *temp1 = [[no componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
                                         componentsJoinedByString:@""];
              if (temp1.length > 10) {
                  NSString *temp=[temp1 substringFromIndex:2];
                  [noArr addObject:temp];
              }
              else
                  [noArr addObject:temp1];
          }
      
          NSLog(@"Arr Data : %@",noArr);
      

      试试这个,绝对对你有帮助。

      【讨论】:

        【解决方案4】:

        试试这个

        for (NSString *str in arrayNumbers) {
            NSString *stringWithoutNineOne = [str stringByReplacingOccurrencesOfString:@"+91" withString:@""];
            NSString *stringWithoutSpace = [stringWithoutNineOne stringByReplacingOccurrencesOfString:@" " withString:@""];
            NSString *stringWithoutdash = [stringWithoutSpace stringByReplacingOccurrencesOfString:@"-" withString:@""];
            [refinedArr addObject:stringWithoutdash];
        }
        

        【讨论】:

          【解决方案5】:

          从数组中删除特殊字符。

          for (NSString *strPhoneNo in arrayPhoneNo) {
              [self removeSpecialCharacter:strPhoneNo];
          }
          

          -(NSString*)removeSpecialCharacter:(NSString*)mobileNumber
          {
              mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+91" withString:@""];
              mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
          
              // remove extra special charecter if needed.
          
              //mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
              //mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
              //mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
          
              return mobileNumber;
          }
          

          如果 NSArray 没有更新,则使用 NSMutableArray insted of NSArray。

          【讨论】:

            【解决方案6】:

            你也可以试试这个...... 获取您给定电话号码的最后 10 位数字.....如果您的要求是固定的,那么试试这个单行代码

            for (NSString *string in arrayNumbers) {
            NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-10, 0)];
            [newArray addobject:trimmedString]
            }
            

            【讨论】:

              猜你喜欢
              • 2012-10-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-02
              • 1970-01-01
              相关资源
              最近更新 更多