2017-02-15 139 views
1

我有一個URL字符串,我需要用它來替換值。替換主字符串中的多個不同的子字符串

例如:

HTTP:\\ www.domain.com \ ID = [USER_ID] &記錄= [CALL_KEY] &一些其它= [OTHER_STUFF]

我需要循環查找[]的字符串,然後用裏面的任何內容替換它。

現在,我必須對每個單獨的密鑰進行硬編碼並將其替換爲值。

- (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; 

} 

我有一堆可以插入不同的值,而不是硬編碼[通話鍵],我想動態讀什麼就在那裏,注入價值。因此初始URL可以是

HTTP:\\ www.domain.com \ ID = [kUsername] &記錄= [kCallKey] &一些其它= [kOtherStuff]

所以我可以通過找到[]的字符串循環,並用裏面的任何內容替換它。

如何搜索字符串,找到[字符,然後該字符串複製到],然後繼續下一個[某某等等?

+0

我會使用正則表達式;得到一個想法,看看這裏:http://stackoverflow.com/questions/27880650/swift-extract-regex-matches –

+2

請參閱[在Objective-C中的正則表達式:如何用動態模板替換匹配?](http:///stackoverflow.com/a/22458626/3832970)。在你的情況下,正則表達式是'@「\\ [(。+?)]」'或'@「\\ [([^ \\] \\ [] +)]」'。 –

回答

0

我用這種方法解決了類似的問題,我用一個字典存儲爲關鍵字的關鍵字被替換,並作爲值替換字符串。

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]; 
     } 
0

試試這個

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); 
相關問題