本帖最后由 变味的面包 于 2015-9-26 14:57 编辑
要求输入: http://www.baidu.com/?d=10&bb=100&cf=22.22&test=&d=20&gg=789 输出结果: d:20 bb:100 cf:22.22 test:null gg:789
搞了一天,还是得不到想要的结果,能将了;里面信息提取出来并保存在一个NSMutableDict中 ,但是得不到输出的格式 ,具体代码如下 - #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[]) {
-
- char a[100];
- char *t = a;
-
- scanf("%s",t);
- NSString *str = [NSString stringWithUTF8String:t];
- // 先从?往后截取
- NSRange range = [str rangeOfString:@"?"];
- NSString *str1 = [str substringFromIndex:(range.location + 1)];
- // NSLog(@"%@",str1); // d=10&bb=100&cf=22.22&test=&d=20&gg=789
-
-
- str1 = [str1 stringByReplacingOccurrencesOfString:@"=&" withString:@"=null&"];
- // dictM了保存数据
- NSMutableDictionary *dictM = [[NSMutableDictionary alloc] initWithCapacity:140];
-
- NSRange ValuRange;
- NSString *valu;
-
- while (ValuRange.location != NSNotFound) {
-
- // 截取key
- NSRange keyRange = [str1 rangeOfString:@"="];
- NSString *key = [str1 substringToIndex:(keyRange.location)];
- // NSLog(@"key = %@",key); // key = d
-
- // 截取 key= 以后的字符串
- NSRange range = [str1 rangeOfString:@"="];
- str1 = [str1 substringFromIndex:(range.location + 1)];
- // NSLog(@"%@",str1); // 10&bb=100&cf=22.22&test=null&d=20&gg=789
-
- // 截取 valu
- ValuRange = [str1 rangeOfString:@"&"];
- if (ValuRange.location == NSNotFound) {
- valu = str1;
- [dictM setObject:valu forKey:key];
- break;
- }
- valu = [str1 substringToIndex:(ValuRange.location)];
- // NSLog(@"valu = %@",valu); // valu = 10
-
- [dictM setObject:valu forKey:key];
-
-
- range = [str1 rangeOfString:@"&"];
- str1 = [str1 substringFromIndex:(range.location + 1)];
- // NSLog(@"%@",str1); // bb=100&cf=22.22&test=null&d=20&gg=789
- }
- // 打印并保存
- NSLog(@"%@",dictM);
-
- BOOL isSussed = [dictM writeToFile:@"/Users/apple/Desktop/Abner/1.plist" atomically:YES];
- NSLog(@"%hhd",isSussed);
-
- return 0;
- }
复制代码
打印结果:- 2015-09-26 14:38:23.893 test[5236:68743] {
- bb = 100;
- cf = "22.22";
- d = 20;
- gg = 789;
- test = null;
- }
复制代码 顺出顺序也不对 ,请问大神这怎么搞?
|