NSString
имеет метод экземпляра stringByReplacingOccurencesOfString:
см. На Apple Documentation for NSString для получения справки о реальном методе - код мудрый вы можете попробовать под кодом внизу.
NSString *s = @"number one and two and three";
NSArray *A1 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSArray *A2 = [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
NSLog(@"s before : %@", s);
// Logs "s before : number one and two and three"
// Lets check to make sure that the arrays are the same length first if not forget about replacing the strings.
if([A1 count] == [A2 count]) {
// Goes into a for loop for based on the number of objects in A1 array
for(int i = 0; i < [A1 count]; i++) {
// Get the object at index i from A1 and check string "s" for that objects value and replace with object at index i from A2.
if([[A1 objectAtIndex:i] isKindOfClass:[NSString class]]
&& [[A2 objectAtIndex:i] isKindOfClass:[NSString class]]) {
// If statement to check that the objects at index are both strings otherwise forget about it.
s = [s stringByReplacingOccurrencesOfString:[A1 objectAtIndex:i] withString:[A2 objectAtIndex:i]];
}
}
}
NSLog(@"s after : %@", s);
// Logs "s after : number 1 and 2 and 3"
Как отмечено в комментариях, этот код может возвращать «кость» как «b1». Чтобы обойти эту проблему можно, я полагаю, заменить массивы:
NSArray *A1 = [NSArray arrayWithObjects:@" one ",@" two ",@" three ", nil];
NSArray *A2 = [NSArray arrayWithObjects:@" 1 ",@" 2 ",@" 3 ", nil];
Примечание Я все, что я сделал добавляется в пространствах, так что если у вас была «кость» не будет заменить его на «b1».
Вы даже взглянули на спецификацию для NSString ??? –