从 NSArray 中删除特定字符串而不是对象

Remove particular String from NSArray not Object

我是 IOS 的新手 development.I 在 NSArray 中得到 phone 个数字,就像这样。

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

现在我想从 NSArray List not whole object.How 中删除 -+91 来做到这一点 请有人帮忙

for (NSString *number in arrayName) {

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


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

}

这样试试:)

编码愉快。

for (NSString *phoneNum in phoneNumArray) {
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"+91" withString:@""];
    phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"-" withString:@""];    
}

试试这个

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

从数组中删除特殊字符。

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 未更新,则使用 NSArray 的 NSMutableArray 代替。

你也可以试试这个.... 获取给定 phone 号码的最后 10 位数字.....如果您的要求已解决,请尝试此单行代码

for (NSString *string in arrayNumbers) {
NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-10, 0)];
[newArray addobject:trimmedString]
}
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);

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