【问题标题】:Character replacing in Xcode - Objective-CXcode 中的字符替换 - Objective-C
【发布时间】:2012-04-10 09:38:49
【问题描述】:

我已使用此代码将“\n”替换为“”,但它不起作用。

我该怎么做?

NSString *descString = [values objectForKey:@"description"];
descString = [descString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

NSLog(@"Description String ---->>> %@",descString);
catlog.description = descString;
[webview loadHTMLString:catlog.description baseURL:nil];
webview.opaque = NO;

【问题讨论】:

标签: iphone objective-c string nsstring


【解决方案1】:

试试这个

NSString *s = @"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"%@", s);

【讨论】:

    【解决方案2】:
    descString = [descString stringByReplacingOccurrencesOfString:@"\\n" withString:@""];
    

    更多信息参考stringByReplacingOccurrencesOfString:withString:方法

    希望对你有所帮助。

    更新

    Swift 3.0

    descString.replacingOccurrences(of: "\\n", with: "");
    

    如果我不拧,它也适用于 Swift 4.0

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多