2013-10-23 162 views

回答

2

这段代码为任何数量的任何国家开展工作:

NSString *string = @"India(IND) United States(US)"; 
NSInteger openParenthesLocation; 
NSInteger closeParenthesLocation; 
do { 
    openParenthesLocation = [string rangeOfString:@"("].location; 
    closeParenthesLocation = [string rangeOfString:@")"].location; 
    if((openParenthesLocation == NSNotFound) || (closeParenthesLocation == NSNotFound)) break; 
    string = [string stringByReplacingCharactersInRange:NSMakeRange(openParenthesLocation, closeParenthesLocation - openParenthesLocation + 1) withString:@""]; 
} while (openParenthesLocation < closeParenthesLocation); 
+0

完美....... –

+0

它的工作:-) – Akshay

0

,因为它除去了一组从字符串的两端部分字符无法使用该功能。

例子:

//Produces "ndia" 
[@"India(IND)" stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"(IND)"]]; 

我建议你使用正则表达式来修剪。

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\([A-Z]*\\)" options:NULL error:nil]; 
NSString *trimmedString = [regex stringByReplacingMatchesInString:sample options:0 range:NSMakeRange(0, [sample length]) withTemplate:@""]; 

正则表达式假设你只有在括号内国家的大写字母。

+0

我的本意不是删除大写字母。只是为了删除palenthesis内的内容。 – Akshay

+1

@Akshay您可以修改正则表达式来括号内的任何内容相匹配,不只是首都 – MadhavanRP

+0

是啊谢谢你,我得到了它 – Akshay