Ios StringByReplacingOfString出现选项问题

Ios StringByReplacingOfString出现选项问题,ios,objective-c,nsstring,Ios,Objective C,Nsstring,我想用其他字符串替换以下格式的字符串。此操作应在for循环中进行 这里fullString看起来像这样 There are some other things ... Child_name_13 and Child_name_12 there are some other things ...... Child_name_1 some other things ... Child_name_12 some other things.. Child_name_6 some other th

我想用其他字符串替换以下格式的字符串。此操作应在for循环中进行

这里fullString看起来像这样

There are some other things ... Child_name_13 and Child_name_12     there are some other things ...... Child_name_1 some other things ... Child_name_12 some other things.. Child_name_6 some other things ... Child_name_7
There are some other things ...Nik and Xin  there are some other things ...... Ben some other things ... Xin some other things.. Mark some other things ... Ayen
我想做的是,用arrOfChildren返回的相关儿童姓名替换“Child_name_1”、“Child_name_12”、“Child_name_7”等

我的做法如下

for(int i=0; i<[arrOfChildren count]; i++){
    NSString *strChildName = [[arrOf15Children objectAtIndex:i] objectForKey:@"Name"];

    fullString = [fullString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"child_name_%d",i+1] withString:strChildName options:NSCaseInsensitiveSearch range:NSMakeRange(0, [fullString length])];
}
但我希望是这样

There are some other things ... Child_name_13 and Child_name_12     there are some other things ...... Child_name_1 some other things ... Child_name_12 some other things.. Child_name_6 some other things ... Child_name_7
There are some other things ...Nik and Xin  there are some other things ...... Ben some other things ... Xin some other things.. Mark some other things ... Ayen

我希望,您将获得该问题。

通过未知数字匹配将获得多个匹配。”“Child_name_10”中确实包含“Child_name_1”,因此通过搜索“Child_name_1”,您将获得多个匹配项

您可以通过“Child_name_1”(注意空格)进行匹配,但必须记住在要替换的名称中添加空格。或者,您可以在“Child_name_x”字符串中插入前导零(因此“Child_name_01”而不是“Child_name_1”等),然后将stringWithFormat更改为:

[NSString stringWithFormat:@"child_name_%02d",i+1]

如果你有99个以上的孩子,做同样的事情,但要有三个数字(SO 033D和'HealthNaMeNe'0.1')等。

< P>你最好换成[ NScStRouthFieldFrase:@ HealthNaveEn%d,i +1 ],用空白的话,可以分开前缀的问题。

< P>也许<强> nRealReasePress < <强> >可以帮助你:

NSString *string = @"Child_name_1 and Child_name_13 are friends";
NSArray *nameArray = @[@"Tom", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"Jerry"];

for (int i = 0; i < [nameArray count]; i++) {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"\\bChild_name_%d\\b", i + 1] options:0 error:nil];
    if (regex != nil) {
        NSTextCheckingResult *firstMatch = [regex firstMatchInString:string options:0 range:NSMakeRange(0, string.length)];
        if (firstMatch) {
            NSRange resultRange = [firstMatch rangeAtIndex:0];
            string = [string stringByReplacingCharactersInRange:resultRange withString:[nameArray objectAtIndex:i]];
        }
    }
}

NSLog(@"result:%@", string);
NSString*string=@“Child_name_1和Child_name_13是朋友”;
NSArray*名称数组=@[“汤姆”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“11”,“杰里];
对于(int i=0;i<[nameArray计数];i++){
NSRegularExpression*regex=[NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@“\\b儿童姓名\%d\\b”,i+1]选项:0错误:无];
if(正则表达式!=nil){
NSTextCheckingResult*firstMatch=[regex firstMatchInString:string选项:0范围:NSMakeRange(0,string.length)];
如果(第一场比赛){
NSRange resultRange=[firstMatch rangeAtIndex:0];
string=[string stringByReplacingCharactersInRange:resultRange with string:[nameArray objectAtIndex:i]];
}
}
}
NSLog(@“结果:%@”,字符串);

此外,您还可以查看NSScanner的更新::)

更新:

刚刚制定了超级简单的解决方案。如果您知道字符串中不包含任何索引大于名称计数的
child_name
,即名称数组中有5个名称,并且字符串中可能存在最大child_name 5和child_name 6+,则肯定不存在

您只需反转迭代名称的方向即可

// only changed for statements, the rest is your code unchanged
for(int i=[arrOfChildren count]-1; i>=0; i--){
    NSString *strChildName = [[arrOf15Children objectAtIndex:i] objectForKey:@"Name"];

    fullString = [fullString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"child_name_%d",i+1] withString:strChildName options:NSCaseInsensitiveSearch range:NSMakeRange(0, [fullString length])];
}
因此,您将替换所有
child\u name15
,然后替换所有
child\u name14
,依此类推,直到
child\u name0
。这就是为什么我们被这些顶级强大的工具宠坏了,我们忘记了应该如何编写代码

旧答案:

你只需要一个正则表达式就可以了。找到所有匹配项并用所需名称替换它们。 可能有效的正则表达式示例:

child_name[0-9]*
例如,您可以通过NSRegularExpression查找所有匹配项,并使用其他内容将其替换为适当的值。另一种解决方案可能是使用组创建更复杂的正则表达式,并使用它们以更优雅的方式实现所需的功能。

问题与:
[NSString stringWithFormat:@“child\u name\u%d”,i+1]

因为一个孩子的名字将与一个孩子的名字相匹配

这个问题有不同的解决方案

1) 多程替换: 首先替换数字大于等于10的项目,然后替换其余的项目

for(int i=9; i<[arrOfChildren count]; i++){
    NSString *strChildName = [[arrOf15Children objectAtIndex:i] objectForKey:@"Name"];

    fullString = [fullString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"child_name_%d",i+1] withString:strChildName options:NSCaseInsensitiveSearch range:NSMakeRange(0, [fullString length])];
}
for(int i=0; i<max(9,[arrOfChildren count]); i++){
    NSString *strChildName = [[arrOf15Children objectAtIndex:i] objectForKey:@"Name"];

    fullString = [fullString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"child_name_%d",i+1] withString:strChildName options:NSCaseInsensitiveSearch range:NSMakeRange(0, [fullString length])];
}
for(int i=9;i
  • 正则表达式解决了获取整个字符串的问题,我们必须使用此\\b儿童\u姓名\u%d\\b
检查以下代码

NSArray *arrOfChildren = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", @"nine", @"ten", @"leven", @"twe", @"thirt", @"fourten", @"fiftee", nil];

NSString *fullString = @"Child_name_1 Child_name_2 Child_name_3 Child_name_4 Child_name_5 Child_name_6 Child_name_7 Child_name_8 Child_name_9 Child_name_10 Child_name_11 Child_name_12  Child_name_13 Child_name_14 Child_name_15  ";

for(int i=0; i<[arrOfChildren count]; i++){
    NSString *strChildName = [arrOfChildren objectAtIndex:i];

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithFormat:@"\\bchild_name_%d\\b",i+1] options:NSRegularExpressionCaseInsensitive error:&error];

    fullString = [regex stringByReplacingMatchesInString:fullString options:0 range:NSMakeRange(0, [fullString length]) withTemplate:strChildName];

}

NSLog(@"full stirng %@", fullString);

}

添加零无法解决问题。因为“Child_name_01”和“Child_name_010”又是一样的???没有任何标准的方法来做到这一点,比如在搜索条件中添加一些选项…@sajaz使用这种格式,它将是“Child_name_10”而不是“Child_name_010”。前导零只在n中没有足够数字时才起作用谢谢你的帮助。我可以用正则表达式来做。检查我的答案,它可以比正则表达式表现得更好。