如何删除id和title中的特殊字符?

时间:2011-03-12 11:41:02

标签: iphone objective-c cocoa cocoa-touch

我希望结果为

id =Afghanistan

title=AF

在括号内,它给出了非英文字符。如何解决它

(

    {
    id = "Afghanistan (\U0627\U0641\U063a\U0627\U0646\U0633\U062a\U0627\U0646)";
    title = "AF\n   \n";
},
    {
    id = "Albania (Shqip\U00ebria)";
    title = "AL\n   \n";
},
    {
    id = "Algeria (\U0627\U0644\U062c\U0632\U0627\U0626\U0631)";
    title = "DZ\n   \n";
},
    {
    id = "American Samoa";
    title = "AS\n   \n";
}

)

2 个答案:

答案 0 :(得分:0)

如果您可以确保可以删除开始括号后的所有内容,它将使一切变得更容易。

// find the location of the bracket
NSInteger bracketStart = [str rangeOfString:@"("].location;
// get the substring to the opening bracket
NSString *reducedStr = [str substringToIndex:bracketStart];
// remove whitespace at the end of the needed string
reducedStr = [reducedStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

您可以删除国家/地区代码中的换行符,如下所示:

NSString *countryCode = [@"AF\n   \n" stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

答案 1 :(得分:0)

NSString* id_str = [NSString stringWithUTF8String: "Afghanistan (\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646)\0"];
id_str = [[id_str componentsSeparatedByString: @"("] objectAtIndex: 0];

NSString* title = @"AS\n   \n";
title = [title stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @"\n "]];