Nikunj Jadav
Nikunj Jadav

Reputation: 3402

NSString format issue

I have NSString like "Circular Quay W, The Rocks NSW, Australia" and I want to display in following string format:

Circular Quay W,
The Rocks NSW,
Australia

so please help me to develop this functionality.

Thanks in advance.

Upvotes: 0

Views: 155

Answers (3)

Ahmad Kayyali
Ahmad Kayyali

Reputation: 8243

replace each comma with (comma and new line ",\n"), if you're showing it in a UILabel make sure to set the height and the number of lines first to 0 using property numberOfLines.

 NSString *stringFoo = @"Circular Quay W, The Rocks NSW, Australia";
 stringFoo = [stringFoo stringByReplacingOccurrencesOfString: @"," withString:@",\n"];

Upvotes: 1

EmptyStack
EmptyStack

Reputation: 51374

Assuming that you are displaying the string in a UILabel.

NSString *str = @"Circular Quay W, The Rocks NSW, Australia";
str = [str stringByReplacingOccurrencesOfString:@", " withString:@",\n"];

Don't forget to set numberOfLines of the label to 0. This lets the label to show multiline text.

label.numberOfLines = 0;

Upvotes: 3

Fran Sevillano
Fran Sevillano

Reputation: 8163

This should do the job:

[NSString stringWithString:@"Circular Quay W,\n The Rocks NSW,\n Australia"];

Cheers

Upvotes: -1

Related Questions