gpt4 book ai didi

ios - 简单的 stringByReplacingOccurrencesOfString 帮助问题

转载 作者:行者123 更新时间:2023-11-28 18:37:48 31 4
gpt4 key购买 nike

我想将字符串中的一些“非法”字符替换为另一个“合法”字符,我有以下代码:

- (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.

最佳答案

来自文档:

stringByReplacingOccurrencesOfString:withString: Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.

-(NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

所以:

aux = [aux stringByReplacingOccurrencesOfString: [NSString stringWithFormat:@"%c",[invalid characterAtIndex:i]] 
withString: [NSString stringWithFormat:@"%c",[valid characterAtIndex:i]]];

关于ios - 简单的 stringByReplacingOccurrencesOfString 帮助问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15805605/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com