2 yue1liang1chuan yue1liang1chuan 于 2013.03.11 16:01 提问

在ios应用中移除“\”符号

在json反响中获取了url字符串,其中有反斜杠。我想删除这些\符号。

我写的代码没有实现:

NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];
    NSString* encodedString = [responseData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"Response ==> %@" ,encodedString);

       // here encodedString is a url which is showing crctly in output and need to trim the \ character.

       // NSString* str = [NSString stringWithFormat: @"encodedString"];
       // str = [str stringByReplacingOccurrencesOfString:@"?" withString:@""];
       // NSLog(@"Response ==> %@" , str);

    encodedString = [encodedString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
        NSLog(@"Response ==> %@" ,encodedString);

请高手指点,谢谢。

3个回答

lanhailvcao
lanhailvcao   2013.03.11 16:52

最好从responseData中移除反斜杠:

NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];

responseData = [responseData stringByReplacingOccurrencesOfString:@" " withString:@""];
responseData = [responseData stringByReplacingOccurrencesOfString:@"\\" withString:@""];
NSString* encodedString = [responseData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
abcdeFGh_xyz
abcdeFGh_xyz   2013.03.11 16:19

试试:

NSMutableString *string=[NSMutableString stringWithString:@"hi this i\\s good"];
//NSMutableString *string=[NSMutableString stringWithString:encodedString];

[string replaceOccurrencesOfString:@"\\" withString:@"" options:1 range:NSMakeRange(0, string.length)];

NSLog(@"%@",string);
u013243469
u013243469   2014.12.15 18:25

查找字符串,在字符串string中查找str1 NSRange range = [string rangeOfString:str1]; range.location为位置 range.length为长度

有了这个,自己写个算法不就行了?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!