0

我有这样的两段

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.

我该如何格式化:

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.

我只知道如何用这样的空格替换 \n:

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

但我不知道如何识别每个段落

谢谢

4

2 回答 2

2

这段代码不是防弹的,需要额外的代码来清理任何无关的换行符,但它应该可以帮助你开始:

// 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);
}
于 2013-02-14T15:52:35.830 回答
1

搜索两个连续的“\n\n”。这会告诉你这是一个段落。

于 2013-02-14T15:47:29.320 回答