1

I have a URL string that I need to replaces values with.

For example:

http:\\www.domain.com\id=[USER_ID]&record=[CALL_KEY]&someother=[OTHER_STUFF]

I need to loop through the string looking for the [ ] then replacing it with whatever is inside it.

As it stands now I have to hard code each individual key and replace it with the value.

- (NSString*) injectValuesIntoURL:(NSString*) url{


    NSString * injected = @"";

    //[CALL KEY]
    injected = [url stringByReplacingOccurrencesOfString:@"[CALL_KEY]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kCallKey"]];

    //[USER_ID]
    injected = [injected stringByReplacingOccurrencesOfString:@"[USER_ID]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kUsername"]];

        //[OTHER_STUFF]
        injected = [injected stringByReplacingOccurrencesOfString:@"[OTHER_STUFF]" withString:[[NSUserDefaults standardUserDefaults] objectForKey:@"kOtherStuff"]];


    return injected;

}

I have a bunch of different values that can be plugged in, and instead of hard coding [CALL KEY], I would like to dynamically read what is in there and inject the value. So the initial URL may be

http:\\www.domain.com\id=[kUsername]&record=[kCallKey]&someother=[kOtherStuff]

So I can just loop through the string finding the [ ] and replacing it with whatever is inside.

How do I search a string and find the [ character and then copy that string up to the ], then continue on to the next [ so and and so forth?

user-44651
  • 3,924
  • 6
  • 41
  • 87
  • I would use regular expressions; to get an idea, look here : http://stackoverflow.com/questions/27880650/swift-extract-regex-matches – Andreas Oetjen Feb 15 '17 at 15:58
  • 2
    See [Regex in Objective-C: how to replace matches with a dynamic template?](http://stackoverflow.com/a/22458626/3832970). The regex in your case is `@"\\[(.+?)]"` or `@"\\[([^\\]\\[]+)]"`. – Wiktor Stribiżew Feb 15 '17 at 16:09

2 Answers2

0

I solved a similar problem this way, I used a dictionary withs stored as keys the keyword to get replaced, and as values the replacing strings.

NSDictionary * stringsReplacedByDictionary = NSDictionary *dictionary = @{
@"kUserName" : Username,
@"kCallKey" : CallKey,
@"kOtherStuff" : otherStuff 
};

// Enumerate the Dictionary containg all Strings to be replaced.
for (NSString *keys in stringsReplacedByDictionary) {

// Here I builded the search string in your case would be [kUsername]
NSString *keysWithParenthesis = [NSString stringWithFormat:@"[$%@]", keys];
NSString *value = [stringsReplacedByDictionary objectForKey:keys];


 //Check if there is a string to replace!
 if ([stringToScan containsString:keysWithParenthesis]) {
 NSRange replacingStringRange = [[textStorage string] rangeOfString:keysWithParenthesis];
 [textStorage replaceCharactersInRange:replacingStringRange withString:value];
        }
voltae
  • 582
  • 4
  • 11
0

Try this

    NSString *url = @"http:\\www.domain.com\\id=[USER_ID]&record=[CALL_KEY]&someother=[OTHER_STUFF]";
    NSArray *values = @[@"one", @"two", @"three"];
    NSString *pattern = @"\\[([^\\]\\[]+)]";

    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil];
    NSInteger count = [regex matchesInString:url options:0 range:NSMakeRange(0, url.length)].count;

    if (count == 0 || count != values.count) {
        // handle error ..
    }

    NSTextCheckingResult *result = [regex firstMatchInString:url options:0 range:NSMakeRange(0, url.length)];

    for (int i = 0; i < values.count; i++) {
        url = [url stringByReplacingCharactersInRange:result.range withString:values[i]];
        result = [regex firstMatchInString:url options:0 range:NSMakeRange(0, url.length)];

        if (result.range.location == NSNotFound) { // optional
            break;
        }
    }

    NSLog(@"%@", url);
schmidt9
  • 4,436
  • 1
  • 26
  • 32