我试图用这样的未知索引替换一些字符(我需要替换这个Ă,Ŏ,Ĭ,ă,ŏ,ĭ,我的输入nsstring可以是任何东西):
(void)repairText:(NSString *)textToRepair{`
NSString *pom = textToRepair;`
int pomNum = [pom length];
NSLog(@"Input nsstring: %@",pom);
for (int a = 0; a<pomNum; a++) {
NSString *pomChar, *pomChar2;
pomChar = [pom substringFromIndex:a];
pomChar2 = [pomChar substringToIndex:(1)];
NSLog(@"Char to repair: %@",pomChar2);
if ([pomChar2 isEqual: @"Ă"] || [pomChar2 isEqual:@"Ŏ"] || [pomChar2 isEqual:@"Ĭ"] || [pomChar2 isEqual:@"ă"] || [pomChar2 isEqual:@"ŏ"] || [pomChar2 isEqual:@"ĭ"]) {
if ([pomChar2 isEqual:@"Ă"]) {
NSLog(@"Wrong big a");
}
if ([pomChar2 isEqual:@"Ŏ"]) {
NSLog(@"Wrong big o");
}
if ([pomChar2 isEqual:@"Ĭ"]) {
NSLog(@"Wrong big i");
}
if ([pomChar2 isEqual:@"ă"]) {
NSLog(@"Wrong small a");
}
if ([pomChar2 isEqual:@"ŏ"]) {
NSLog(@"Wrong small o");
}
if ([pomChar2 isEqual:@"ĭ"]) {
NSLog(@"Wrong small i");
}
} else {
NSLog(@"Good");
}
}
pom = [textToRepair stringByReplacingOccurrencesOfString:@"•" withString:@" kulka "];
pom = [textToRepair stringByReplacingOccurrencesOfString:@"¥" withString:@" jen "];
pom = [textToRepair stringByReplacingOccurrencesOfString:@"£" withString:@" libra "];
pom = [textToRepair stringByReplacingOccurrencesOfString:@"€" withString:@" euro "];
[self synthesize:pom];
}
但是我在'if'时遇到了麻烦。如果有人知道这一点,请在这方面提供帮助。
NSString *str=@"ĂdsdaĬsd";
str=[str stringByReplacingOccurrencesOfString:@"Ă" withString:@""];
str=[str stringByReplacingOccurrencesOfString:@"Ĭ" withString:@""];
NSLog(@"%@",str);
O / p:dsdasd
NSString
有能力为您做到这一点。 dataUsingEncoding:allowLossyConversion:
是你需要的方法。
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)flag
如果flag为YES且接收器无法在不丢失某些信息的情况下进行转换,则某些字符可能会在转换时被删除或更改。例如,在将字符从NSUnicodeStringEncoding转换为NSASCIIStringEncoding时,字符'Á'变为'A',失去重音。
示例代码:
NSString *str = @"á, é, í, ó, ú, ü, ñ";
NSData *asciiStringData = [str dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *finalString = [[NSString alloc] initWithData:asciiStringData
encoding:NSASCIIStringEncoding];
最后一个字符串将是:a, e, i, o, u, u, n
NSString * textToRepair = @"Your String";
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:@"•" withString:@"kulka"];
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:@"¥" withString:@"jen"]
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:@"£" withString:@"libra"];
textToRepair =[textToRepair stringByReplacingOccurrencesOfString:@"€" withString:@"euro"];;
现在textToRepair是带有更改的输出字符串。
你可以用这个:
NSString * myString = @"Hello,";
NSString * newString = [myString stringByReplacingOccurrencesOfString:@“,”withString:@“”];
的NSLog(@ “%@ XX”,newString);
我希望这对你充分利用。