NSString remove Brackets

YosiFZ

I am using this function to remove Brackets and inside them:

+(NSString*)removeCharsBetweenBrackets:(NSString*)str {
NSRange range = [str rangeOfString:@"("];
if (range.location != NSNotFound) {
    NSRange range2 = [str rangeOfString:@")"];

    if (range2.location != NSNotFound) {
        NSString *str1 = [str substringToIndex:range.location];
        NSString *str2 = [str substringFromIndex:range2.location + 1];
        str = [NSString stringWithFormat:@"%@ %@",str1,str2];
    }
}

range = [str rangeOfString:@"["];
if (range.location != NSNotFound) {
    NSRange range2 = [str rangeOfString:@"]"];

    if (range2.location != NSNotFound) {
        NSString *str1 = [str substringToIndex:range.location];
        NSString *str2 = [str substringFromIndex:range2.location + 1];
        str = [NSString stringWithFormat:@"%@ %@",str1,str2];
    }
}

return str;
}

I called it twice to remove twice ,And it remove it perfectly.

The issue is when i have string like this:

Mystring(blablabla)(*).mp3

* - Is a number. can be 0-999

And i want to remove only the (*).

How i can implement it?

Edit:

The string can be :

mystring(bla bla)(1).mp3
mystring(bla bla)(1123).mp3
mystring(99).mp3
mystring(9).mp3
mystring.mp3
mystring(bla bla).mp3

And i need to remove the (number) if it's exist.

Sulthan
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"\\([0-9]{1,3}\\)" options:0 error:NULL];

NSString *result = [regexp stringByReplacingMatchesInString:string
                                                    options:0
                                                      range:NSMakeRange(0, [string length])
                                               withTemplate:@""];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related