use*_*878 1 objective-c nsstring ios4 ios
我正在尝试查找单词列表,如果匹配我正在替换.打击代码有效,但如果匹配的单词出现多次,则不会替换.
我认为我需要使用while而不是if循环,但我无法使其工作.
我在挣扎请告诉我
NSString *mymessage = @"for you for your information at you your at fate";
NSMutableArray *full_text_list = [[NSMutableArray alloc]init];
[full_text_list addObject:@"for"];
[full_text_list addObject:@"for your information"];
[full_text_list addObject:@"you"];
[full_text_list addObject:@"at"];
NSMutableArray *short_text_list = [[NSMutableArray alloc]init];
[short_text_list addObject:@"4"];
[short_text_list addObject:@"fyi"];
[short_text_list addObject:@"u"];
[short_text_list addObject:@"@"];
for(int i=0;i<[full_text_list count];i++)
{
NSRange range = [mymessage rangeOfString:[full_text_list objectAtIndex:i]];
if(range.location != NSNotFound) {
NSLog(@"%@ found", [full_text_list objectAtIndex:i]);
mymessage = [mymessage stringByReplacingCharactersInRange:range withString:[short_text_list objectAtIndex:i]];
}
}
Run Code Online (Sandbox Code Playgroud)
Dr.*_*eon 15
你不必重新发明轮子; 可可为你做到了......
代码:
NSString* message = @"for you for your information at you your at fate";
NSMutableArray* aList = [[NSMutableArray alloc] initWithObjects:@"for your information",@"for",@"you ",@"at ",nil];
NSMutableArray* bList = [[NSMutableArray alloc] initWithObjects:@"fyi",@"4",@"u ",@"@ ",nil];
for (int i=0; i<[aList count];i++)
{
message = [message stringByReplacingOccurrencesOfString:[aList objectAtIndex:i]
withString:[bList objectAtIndex:i]];
}
NSLog(@"%@",message);
Run Code Online (Sandbox Code Playgroud)
提示: 我们将用bList [0]替换aList [0]的每一个出现,用bList [1]替换aList [1],依此类推...... ;-)
| 归档时间: |
|
| 查看次数: |
9182 次 |
| 最近记录: |