2 starwar2012 starwar2012 于 2012.12.28 09:49 提问

iphone-字符串中替换/”

我的字符串如下:

"phone" : "555-522-8243",
"name" : "Anna Haro"

我想把"全都换成/",怎么样实现呢?

2个回答

gaoXxxing
gaoXxxing   2012.12.28 11:10
已采纳
NSString* yourNewString=  [yourOldaString stringByReplacingOccurrencesOfString:@"\"" withString:@"/\""];
NSLog(@"\n New String %@",yourNewString);
starwar2012
starwar2012 谢谢
接近 5 年之前 回复
a21064346
a21064346   2012.12.28 10:29

试试看,我没具体调试。
其他办法暂时没想出来

NSString *sourceString = nil;
    NSMutableString *resultString = nil;
    int length = [sourceString length];
    for (int i = 0;i<length;i++) {
        NSRange range ;
        range.location =i;
        range.length =1;
        if ([[sourceString substringWithRange:range] isEqualToString:@"\""]) {
            [resultString appendFormat:@"\\\""];
        }else{
            [resultString appendString:[sourceString substringWithRange:range]];
        }
    }

    //method 2
    NSArray *subStrings = [sourceString componentsSeparatedByString:@"\""];
    NSString *new = [subStrings componentsJoinedByString:@"\\\""];

//method3
    NSString *str = nil;
    [str stringByReplacingOccurrencesOfString:<#(NSString *)#> withString:<#(NSString *)#>]
starwar2012
starwar2012 谢谢
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!