0

I have a string with certain pattern. I need to search for the pattern and replace the string inside that pattern. For eg :

NSString *string = @"{Hello} ({World}) ({How}) ({Are}) ({You})";
NSString *result = nil;

// Determine "{" location
NSRange startRange = [string rangeOfString:@"{" options:NSCaseInsensitiveSearch];
if (startRange.location != NSNotFound)
{
    // Determine "}" location according to "{" location
    NSRange endRange;

    endRange.location = startRange.length + startRange.location;
    endRange.length   = [string length] - endRange.location;
    endRange = [string rangeOfString:@"}" options:NSCaseInsensitiveSearch range:endRange];

    if (endRange.location != NSNotFound)
    {
        // bracets found: retrieve string between them
        startRange.location += startRange.length;
        startRange.length = endRange.location - startRange.location;

        result = [string substringWithRange:startRange];

    }
}

Here I am able to extract the first substring that is between "{ }" ie - "Hello" but I also need to continue the check and want to extract other strings.

3 Answers 3

2

Try this one:

NSString *string = @"{Hello} ({World}) ({How}) ({Are}) ({You})";
    //NSString *result = nil;

    // Determine "{" location

    NSArray *array=[string componentsSeparatedByString:@"{"];
    for(NSString *str in array){
        NSString *newString=[[str componentsSeparatedByString:@"}"] objectAtIndex:0];
    NSLog(@"%@",newString);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

try this :

NSString *string = @"{Hello} ({World}) ({How}) ({Are}) ({You})";
    NSMutableString *result = [[NSMutableString alloc] init];

    NSArray *tempArray = [[string componentsSeparatedByString:@" "] mutableCopy];

    for (int i=0; i < [tempArray count]; i++)
    {
        NSString *tempStr = [tempArray objectAtIndex:i];
        NSRange startRange = [tempStr rangeOfString:@"{" options:NSCaseInsensitiveSearch];
        if (startRange.location != NSNotFound)
        {
            // Determine "}" location according to "{" location
            NSRange endRange;

            endRange.location = startRange.length + startRange.location;
            endRange.length   = [tempStr length] - endRange.location;
            endRange = [tempStr rangeOfString:@"}" options:NSCaseInsensitiveSearch range:endRange];

            if (endRange.location != NSNotFound)
            {
                // bracets found: retrieve string between them
                startRange.location += startRange.length;
                startRange.length = endRange.location - startRange.location;

                //result = [tempStr substringWithRange:startRange];
                [result appendString:[NSString stringWithFormat:@"%@ ",[tempStr substringWithRange:startRange]]];
                NSLog(@"%@ ",result);

            }
        }
    }

Take care for release for tempArray and result

Comments

0

I happen to have this code lying around. I think it does exactly what you want. I implemented it as a category on NSString. You use it like this:

NSString *template = @"{Hello} ({World}) ({How}) etc etc";
NSDictionary *vars = [NSDictionary dictionaryWithObjectsAndKeys:
    @"Bonjour", @"Hello",
    @"Planet Earth", @"World",
    @"Como", @"How",
    // etc.
    nil];

NSString *expandedString = [template stringByExpandingTemplateWithVariables:vars];
// expandedString is @"Bonjour (Planet Earth) (Como) etc etc"

Here's the code.

File NSString+TemplateExpansion.h

#import <Foundation/Foundation.h>

@interface NSString (TemplateExpansion)

- (NSString *)stringByExpandingTemplateWithVariables:(NSDictionary *)dictionary;

@end

File NSString+TemplateExpansion.m

#import "NSString+TemplateExpansion.h"

@implementation NSString (TemplateExpansion)

- (NSString *)stringByExpandingTemplateWithVariables:(NSDictionary *)dictionary
{
    NSUInteger myLength = self.length;
    NSMutableString *result = [NSMutableString stringWithCapacity:myLength];

    NSRange remainingRange = NSMakeRange(0, myLength);
    while (remainingRange.length > 0) {
        NSRange leftBraceRange = [self rangeOfString:@"{" options:0 range:remainingRange];
        if (leftBraceRange.location == NSNotFound)
            break;

        NSRange afterLeftBraceRange = NSMakeRange(NSMaxRange(leftBraceRange), myLength - NSMaxRange(leftBraceRange));
        NSRange rightBraceRange = [self rangeOfString:@"}" options:0 range:afterLeftBraceRange];
        if (rightBraceRange.location == NSNotFound)
            break;

        NSRange beforeLeftBraceRange = NSMakeRange(remainingRange.location, leftBraceRange.location - remainingRange.location);
        [result appendString:[self substringWithRange:beforeLeftBraceRange]];
        remainingRange = NSMakeRange(NSMaxRange(rightBraceRange), myLength - NSMaxRange(rightBraceRange));

        NSRange keyRange = NSMakeRange(NSMaxRange(leftBraceRange), rightBraceRange.location - NSMaxRange(leftBraceRange));
        NSString *key = [self substringWithRange:keyRange];
        NSString *value = [dictionary objectForKey:key];
        if (value)
            [result appendString:value];
    }

    [result appendString:[self substringWithRange:remainingRange]];
    return result;
}

@end

Comments

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.