用新行替换NSString中的<br/>

时间:2012-08-23 14:30:53

标签: ios nsstring line-breaks

我有NSString这样的

NSString *string = @"textTextTextTextText<br>textTextTextText<br>TextTextText"

我想将此NSString设置为UICell的文本,并在字符串上找到的每个标签上添加一个新行。我怎么能这样做?

我试过这个,没有成功:

cell.textLabel.text = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@"<br>"];

3 个答案:

答案 0 :(得分:12)

怎么样:

string = [string stringByReplacingOccurrencesOfString: @"<br>" withString: @"\n"]

或者,如果您使用的是Swift Strings

var string = "textTextTextTextText<br>textTextTextText<br>TextTextText"
string = Array(string).reduce("") {$0 + ($1 == "<br>" ? "\n" : $1)}

答案 1 :(得分:2)

NSString * result =
  [string stringByReplacingOccurrencesOfString:@"<br>" withString:@"\n"];

答案 2 :(得分:0)

SWIFT 5: 如果您使用的是Swift字符串(可以将NSString转换为String):

df2