zoukankan      html  css  js  c++  java
  • iOS获取/删除url中的参数

    1.获取URL中的某个参数:

    - (NSString *)getParameter:(NSString *)parameter urlStr:(NSString *)url {
        
        NSError *error;
        
        if (!url) {
            
            return@"";
            
        }
        
        NSString *regTags = [[NSString alloc] initWithFormat:@"(^|&|\?)+%@=+([^&]*)(&|$)",parameter];
        
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags options:NSRegularExpressionCaseInsensitive error:&error];
        
        NSArray *matches = [regex matchesInString:url options:0 range:NSMakeRange(0, [url length])];
        
        for (NSTextCheckingResult *match in matches) {
            
            NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]]; //分组2所对应的串
            
            return tagValue;
            
        }
        
        return@"";
        
    }

    2.删除URL中的某个参数:

    - (NSString *)deleteParameter:(NSString *)parameter WithOriginUrl:(NSString *)originUrl {
    
        NSString *finalStr = [NSString string];
        
        NSMutableString * mutStr = [NSMutableString stringWithString:originUrl];
        
        NSArray *strArray = [mutStr componentsSeparatedByString:parameter];
        
        NSMutableString *firstStr = [strArray objectAtIndex:0];
        
        NSMutableString *lastStr = [strArray lastObject];
        
        NSRange characterRange = [lastStr rangeOfString:@"&"];
    
        if (characterRange.location !=NSNotFound) {
    
            NSArray *lastArray = [lastStr componentsSeparatedByString:@"&"];
            
            NSMutableArray *mutArray = [NSMutableArray arrayWithArray:lastArray];
            
            [mutArray removeObjectAtIndex:0];
            
            NSString *modifiedStr = [mutArray componentsJoinedByString:@"&"];
            
            finalStr = [[strArray objectAtIndex:0]stringByAppendingString:modifiedStr];
            
        } else {
            
            //以'?'、'&'结尾
            finalStr = [firstStr substringToIndex:[firstStr length] -1];
            
        }
    
        return finalStr;
        
    }
  • 相关阅读:
    codeforces 57C 思维
    FOJ 2232 匈牙利算法找二分图最大匹配
    UVA 101 vector
    POJ 3070 矩阵mob
    codeforces 60B bfs
    codeforces 54A
    codeforces 466C 计数 codeforces 483B 二分 容斥
    FOJ 2213 简单几何
    CCF-最优配餐(BFS)
    杂论-FTP
  • 原文地址:https://www.cnblogs.com/huzhenchao/p/6898821.html
Copyright © 2011-2022 走看看