Nam Vu
Nam Vu

Reputation: 5725

Re make multiple line to paragraph in objective c

i have this 2 paragraphs like this

On Friday afternoon after you leave work, you probably
think about going out and having a few drinks with 
friends to relax and wind down.  Even though you 
may think you deserve to go out and have a few drinks,
there are some things that you should certainly keep
in mind.

Like any other day, tomorrow is going to be a day
for exercise, and since you are exercising on a 
regular basis, a few drinks of alcohol won't really
hurt anything, right?  Before you decide to rush out
to the local bar, there are a few things below that
you should think about before you make your choice
about going out to drink some alcohol.

How can i format to this:

On Friday afternoon after you leave work, you probably think about going out and having a few drinks with friends to relax and wind down. Even though you may think you deserve to go out and have a few drinks, there are some things that you should certainly keep in mind.

Like any other day, tomorrow is going to be a day for exercise, and since you are exercising on a regular basis, a few drinks of alcohol won't really hurt anything, right? Before you decide to rush out to the local bar, there are a few things below that you should think about before you make your choice about going out to drink some alcohol.

I only know how to replace \n with space like this:

stringByReplacingOccurrencesOfString:@"\n" withString:@" "

But i dont know how to recognize each paragraph

Thanks you

Upvotes: 0

Views: 123

Answers (2)

trojanfoe
trojanfoe

Reputation: 122411

This code isn't bulletproof and additional code is needed to clean up any extraneous newline characters, but it should serve to start you off:

// Assuming your input string contains all the text, then:

NSArray *paragraphs = [input componentsSeparatedByString:@"\n\n"];
for (NSString *paragraph in paragraphs)
{
    NSString *cleaned = [paragraph stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
    NSLog(@"paragraph = %@", cleaned);
}

Upvotes: 2

Kirby Todd
Kirby Todd

Reputation: 11556

Search for two consecutive "\n\n". This will tell you it's a paragraph.

Upvotes: 1

Related Questions