Xcode中的字符替换 - Objective-C

问题描述 投票:2回答:2

我已经使用此代码将“\ 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
2个回答
5
投票
descString = [descString stringByReplacingOccurrencesOfString:@"\\n" withString:@""];

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

希望这对你有所帮助。

UPDATE

Swift 3.0

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

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


10
投票

试试这个

NSString *s = @"foo/bar:baz.foo";
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
s = [[s componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];
NSLog(@"%@", s);
© www.soinside.com 2019 - 2024. All rights reserved.