0

I have a bunch of strings, and I want to always get the string inbetween the { and } including the braces. So if I have

@"alsdkfj {'asdf':'stuff stuff'} more stuff"

I want output

@"{'asdf':'stuff stuff'}"

How could I do this? The string is always different so I cant just search for "alsdkfj", I need regarless of what it is to get inbetween the {}

2 Answers 2

2

You can get multiple subString using following code.

  NSString* placeHolderString = @"alsdkfj {'asdf':'stuff stuff'} more stuff";
    NSRegularExpression* faceRegexQuota = [[NSRegularExpression alloc] initWithPattern:@"\\{(.*?)\\}" options:0 error:NULL];
    [faceRegexQuota enumerateMatchesInString:placeHolderString options:0 range:NSMakeRange(0, [placeHolderString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){

        NSString* matchText = [placeHolderString substringWithRange:[match range]];
        NSLog(@"Output : %@",matchText);

    }];

Output : {'asdf':'stuff stuff'}

Sign up to request clarification or add additional context in comments.

1 Comment

@jamesbrown, let me know if you stuck anywhere.. this is dynamic return number of substring that match with your whole string
1

Use the following code.

NSString *finalString = @"alsdkfj {'asdf':'stuff stuff'} more stuff";
NSRange range2 = [content rangeOfString:@"{"];
NSRange range3 = [content rangeOfString:@"}"];
int lengt = range3.location - range2.location - range2.length;
int location = range2.location + range2.length;
NSRange range4;
range4.location = location;
range4.length = lengt;
finalString = [finalString substringWithRange:range4];
finalString = [NSString stringWithFormat:@"{%@}",finalString];

2 Comments

I want it to include the { at the begging in and the } at the end
@jamesbrown Please check the update. You can easily include the braces yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.