iphone:问题替换字符串

时间:2011-08-24 07:03:43

标签: iphone objective-c ios4

NSString *re = [[sqlArray objectAtIndex:0] valueForKey:@"Question"];
            question = [re stringByReplacingOccurrencesOfString:@"í" withString:@"'"];

            question = [re stringByReplacingOccurrencesOfString:@"î" withString:@"'"];
         question = [re stringByReplacingOccurrencesOfString:@"í" withString:@"'"];
        question = [re stringByReplacingOccurrencesOfString:@"ì" withString:@"'"];
        question = [re stringByReplacingOccurrencesOfString:@"ë" withString:@"'"];
        NSLog(@"question : %@",question);

字符串仍然没有被替换,它显示

问题:逗留时间最长的居民√≠通常是那些

的人

作为nslog。 什么可能是错的?

2 个答案:

答案 0 :(得分:3)

试试这个:

NSString *re = [[sqlArray objectAtIndex:0] valueForKey:@"Question"];
            question = [re stringByReplacingOccurrencesOfString:@"í" withString:@"'"];

            question = [question stringByReplacingOccurrencesOfString:@"î" withString:@"'"];
         question = [question stringByReplacingOccurrencesOfString:@"í" withString:@"'"];
        question = [question stringByReplacingOccurrencesOfString:@"ì" withString:@"'"];
        question = [question stringByReplacingOccurrencesOfString:@"ë" withString:@"'"];
        NSLog(@"question : %@",question);

答案 1 :(得分:0)

当您重复设置question = ...时,您只是更改了局部变量而不是Question键的值。所以用

完成这个
[[sqlArray objectAtIndex:0] setObject:question forKey:@"Question"];

(假设这是一个NSDictionary;也许你应该使用setValue:forKey:,但是如果没有看到更多的代码,我就无法分辨。)

此外,正如joern指出的那样,您应该在第一次替换后使用question = [question stringBy...而不是question = [re stringBy...