Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 替换主字符串中的多个不同子字符串_Ios_Objective C_String - Fatal编程技术网

Ios 替换主字符串中的多个不同子字符串

Ios 替换主字符串中的多个不同子字符串,ios,objective-c,string,Ios,Objective C,String,我有一个URL字符串,需要用它替换值 例如: http:\\www.domain.com\id=[USER\u id]&record=[CALL\u KEY]&someother=[OTHER\u STUFF] 我需要在字符串中循环查找[],然后用其中的内容替换它 现在,我必须对每个键进行硬编码,并将其替换为值 - (NSString*) injectValuesIntoURL:(NSString*) url{ NSString * injected = @""; //[C

我有一个URL字符串,需要用它替换值

例如:

http:\\www.domain.com\id=[USER\u id]&record=[CALL\u KEY]&someother=[OTHER\u 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;

}
我有很多不同的值可以插入,我不想硬编码[CALL KEY],而是想动态读取其中的内容并注入值。因此,初始URL可能是

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

所以我可以循环遍历字符串,找到[]并用其中的任何内容替换它


如何搜索字符串并找到[字符,然后将该字符串复制到],然后继续搜索下一个字符串[如此等等?

我通过这种方式解决了一个类似的问题,我使用了一个字典,它将关键字存储为关键字,替换字符串作为值

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

我通过这种方式解决了一个类似的问题,我使用了一个字典,它将关键字存储为关键字,将替换字符串存储为值

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];
        }
试试这个

    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);
NSString*url=@“http:\\www.domain.com\\id=[USER\u id]&record=[CALL\u KEY]&someother=[OTHER\u STUFF]”;
NSArray*值=@[@“一”、“二”、“三”];
NSString*模式=@“\\[([^\\]\\[]+)]”;
NSRegularExpression*regex=[[NSRegularExpression alloc]initWithPattern:pattern选项:0错误:nil];
NSInteger count=[regex matchesisnstring:url选项:0范围:NSMakeRange(0,url.length)].count;
if(count==0 | | count!=values.count){
//处理错误。。
}
NSTextCheckingResult*result=[regex firstMatchInString:url选项:0范围:NSMakeRange(0,url.length)];
对于(int i=0;i
试试这个

    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);
NSString*url=@“http:\\www.domain.com\\id=[USER\u id]&record=[CALL\u KEY]&someother=[OTHER\u STUFF]”;
NSArray*值=@[@“一”、“二”、“三”];
NSString*模式=@“\\[([^\\]\\[]+)]”;
NSRegularExpression*regex=[[NSRegularExpression alloc]initWithPattern:pattern选项:0错误:nil];
NSInteger count=[regex matchesisnstring:url选项:0范围:NSMakeRange(0,url.length)].count;
if(count==0 | | count!=values.count){
//处理错误。。
}
NSTextCheckingResult*result=[regex firstMatchInString:url选项:0范围:NSMakeRange(0,url.length)];
对于(int i=0;i
我会使用正则表达式;要想知道,请看这里:请看。您案例中的正则表达式是
@“\\[(.+?)]”“
@”\[([^\\\]\[]+)]”“
。我会使用正则表达式;要想知道,请看这里:请看。您案例中的正则表达式是
@“\\\[(.+?)]”“
\\[([^\\\\]\[])”