如何用未知索引替换NSString中的特殊字符

时间:2014-04-17 09:12:46

标签: ios objective-c nsstring

我试图用这样的未知索引替换一些字符(我需要替换这个Ă,Ŏ,Ĭ,ă,ŏ,ĭ,我的输入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'时遇到了麻烦。如果有人知道这一点,请在这方面提供帮助。

4 个答案:

答案 0 :(得分:3)

NSString *str=@"ĂdsdaĬsd";
str=[str stringByReplacingOccurrencesOfString:@"Ă" withString:@""];
str=[str stringByReplacingOccurrencesOfString:@"Ĭ" withString:@""];
NSLog(@"%@",str);

O / p:dsdasd

答案 1 :(得分:1)

NSString具有为您执行此操作的功能。 dataUsingEncoding:allowLossyConversion:是您需要的方法。

来自documentation

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

答案 2 :(得分:0)

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是带有更改的输出字符串。

答案 3 :(得分:0)

你可以用这个:

NSString * myString = @"Hello,";

NSString * newString = [myString stringByReplacingOccurrencesOfString:@“,”withString:@“”];

的NSLog(@ “%@ XX”,newString);

我希望这完全用于你。