Lucas Pereira
Lucas Pereira

Reputation: 569

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>"];

Upvotes: 3

Views: 9418

Answers (3)

Michael_Gruszka
Michael_Gruszka

Reputation: 43

SWIFT 5: If you are using Swift string (you can transform your NSString to String):

var string = "line1<br>line2<br>line3"
string = string..replacingOccurrences(of: "<br>", with: "\n"))

Upvotes: 0

Ashley Mills
Ashley Mills

Reputation: 53181

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)}

Upvotes: 13

justin
justin

Reputation: 104698

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

Upvotes: 2

Related Questions