2013-10-23 2 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

PErfect ....... –

+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

제 의도는 대문자를 제거하지 않습니다. 단지 paranthesis 안의 내용물을 제거하기 위해서입니다. – Akshay

+1

@Akshay 대문자뿐만 아니라 괄호 안의 내용에 맞게 정규 표현식을 수정할 수 있습니다. – MadhavanRP

+0

네, 고맙습니다. – Akshay