Xcode中的字符替换 - Objective-C

时间:2012-04-10 09:38:49

标签: iphone objective-c string nsstring

我已经使用此代码将“\ 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;

2 个答案:

答案 0 :(得分:10)

试试这个

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

答案 1 :(得分:5)

descString = [descString stringByReplacingOccurrencesOfString:@"\\n" withString:@""];

有关更多信息,请参阅stringByReplacingOccurrencesOfString:withString:方法

希望这会对你有所帮助。

<强>更新

Swift 3.0

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

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