user1927992
user1927992

Reputation: 123

Adding \n to the end of each line

Hello I have this very simple app. In my main window, there is a NSTextView and I am able to get text from it. It is meant so that text pasted in it will be 1 sentence each line. There is also a button and by the click of the button, the text that was pasted in the NSTextView will add a "\n" to the end of each line. How do I do that?

EX. Pasted Hello Stackoverflow. I am having a good day.

After formatted Hello Stackoverflow.\n I am having a good day.\n

Thanks!

Upvotes: 1

Views: 338

Answers (2)

Forhad Ahmed
Forhad Ahmed

Reputation: 1771

You could replace each occurrance of a period in your input text '.' with a period followed by a newline '.\n':

NSString *str = @"Hello Stackoverflow. I am having a good day.";

str = [str stringByReplacingOccurrencesOfString:@"."
                                     withString:@".\n"];

This is a very simplistic example which assumes all your lines end with a period and there are no periods anywhere inside a line of text

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122391

Sounds like you just need to substitute every . character with .\n although there will be exceptions, for example if an IP address or floating-point number is entered, it would be messed up.

But in the general case:

NSString *transformed = [input stringByReplacingOccurrencesOfString:@"."
                                                         withString:@".\n"];

Upvotes: 2

Related Questions