我想将字符串中的一些“非法”字符替换为另一个“合法”字符,我有以下代码:
- (NSString *) quitarTildes:(NSString *)aStr
{
NSString *aux = aStr;
NSString *invalid = @"áéíóúÁÉÍÓÚñÑ";
NSString *valid = @"aeiouAEIOUnN";
for (int i = 0; i < [invalid length]; i++)
[aux stringByReplacingOccurrencesOfString: [NSString stringWithFormat:@"%c",[invalid characterAtIndex:i]]
withString: [NSString stringWithFormat:@"%c",[valid characterAtIndex:i]]];
NSLog(@"%@",aux);
return aux;
}
所有的非法字符仍然存在......
NSString *test = @"Hólá, Ésto es una prueba.";
test = [self quitarTildes: test];
NSLog,响应:
2013-04-04 08:58:52.896 GeoRuta_v1[1960:907] Hólá, Ésto es una prueba.