Search code examples
iosnsstringline-breaks

Replace <br> in an NSString with a new line


I have an NSString like this

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

I want to set this NSString to be the text of my UICell with a new line on each
tag found on the string. How could I do that?

I've tried this, without success:

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


Solution

  • How about:

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

    or, if you're using Swift Strings

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