How can I remove any substring that has parentheses () in my NSString?
Asked Answered
S

3

5

I have the following NSString:

Hello (my name is) John

How do I remove the (my name is) part quickly and easily? I want to make sure it handles situations where there is only a ( and then a following ) and doesn't crash on anything else.

Siu answered 4/6, 2013 at 19:52 Comment(4)
Check out #2493653Cagle
Just find the index of the ( and ) characters and remove everything in between (hey, this is just really is what you asked).Bilbrey
Also, you probably will have to use some sort of recursion if your parentheses can be embedded in each other.Bilbrey
you could use regexp for that.Rape
B
8

If you want to remove text between parentheses, then... well, remove text between parentheses.

NSMutableString *s = [NSMutableString stringWithString:@"Hello (my name is) John"];
NSRange start = [s rangeOfString:@"("];
NSRange end = [s rangeOfString:@")"];
[s deleteCharactersInRange:(NSRange){ start.location, end.location - start.location + 1}];

(repeat process until there are parens)

Bilbrey answered 4/6, 2013 at 20:1 Comment(3)
@SanjitSaluja Oh come on! You saw the example? "Hello (my name is) John"Bilbrey
I believe this may throw when end < start.Polyanthus
this will certainly crash if you don't check that a variable string isn't nil or @"" ... in this case, you're hardcoding a string so it won't crash, but for many who read this post, you have to check that your string is valid first.Dissection
C
6

Easy to do using regular expressions (greedy):

NSError *error = NULL;
NSString *stringToBeReplaced = @"Hello (my name is) John";
NSString *regexString = @"\\(.*\\)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:stringToBeReplaced options:0 range:NSMakeRange(0, [stringToBeReplaced length]) withTemplate:@""];
// Greedy means it will match "My name (is John) (Jobs)." => "My name ."

For a non-greedy regular expression use:

NSString *regex = @"\\(.*?\\)";
Cimabue answered 4/6, 2013 at 20:27 Comment(3)
This is quite an elegant and powerful solution. But if your goal is strictly what was asked (no more, no less), I'd go with H2CO3's solution. Much more simple and probably an order of magnitude faster (if it matters to you).Enliven
I hope you are not going to advise the use of regular expressions when next time one wants to parse XML... (Honestly, there are cases when regexes are appropriate, but I don't believe this is such a situation.)Bilbrey
Regexes are not too good for paren-matching. This has other drawbacks on degenerate input different from H2CO3's solution. It will match two words in "special case (parentheses (mismatched)". That might or might not be what you want.Abelmosk
U
0

If your text has more than one occurrence of () you could try something like:

-(NSString *)clearString:(NSString *)stringToClear {
     while([stringToClear rangeOfString:@"("].location != NSNotFound) {
          NSRange firstRange = [stringToClear rangeOfString:@"("];
          NSRange secondRange = [stringToClear rangeOfString:@")"];
          stringToClear = [stringToClear stringByReplacingCharactersInRange:
                                 NSMakeRange(firstRange.location, secondRange.location)
                        withString:@""];
     }
     return  stringToClear;
}
Ululate answered 4/6, 2013 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.