08. Words, Paragraphs, and Line Breaks

相關(guān)鏈接:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/stringsParagraphBreaks.html#//apple_ref/doc/uid/TP40005016-SW1

This article describes how word and paragraph boundaries are defined, how line breaks are represented, and how you can separate a string by paragraph.

  • 本文介紹如何定義單詞和段落邊界书蚪,如何表示換行符庶诡,以及如何按段落分隔字符串。

Word Boundaries

The text system determines word boundaries in a language-specific manner according to Unicode Standard Annex #29 with additional customization for locale as described in that document. On OS X, Cocoa presents APIs related to word boundaries, such as the NSAttributedString methods doubleClickAtIndex: and nextWordFromIndex:forward:, but you cannot modify the way the word-boundary algorithms themselves work.

  • 文本系統(tǒng)根據(jù)Unicode標(biāo)準(zhǔn)附件#29以特定語言的方式確定字邊界,并對(duì)該文檔中描述的區(qū)域設(shè)置進(jìn)行額外定制朝刊。 在OS X上,Cocoa提供了與字邊界相關(guān)的API,例如NSAttributedString方法doubleClickAtIndex:和nextWordFromIndex:forward:,但是您無法修改字邊界算法本身的工作方式坯门。

Line and Paragraph Separator Characters

  • 線和段落分隔符

There are a number of ways in which a line or paragraph break can be represented. Historically, \n, \r, and \r\n have been used. Unicode defines an unambiguous paragraph separator, U+2029 (for which Cocoa provides the constant NSParagraphSeparatorCharacter), and an unambiguous line separator, U+2028 (for which Cocoa provides the constant NSLineSeparatorCharacter).

  • 可以通過多種方式表示行或段落。 從歷史上看逗扒,已使用\ n古戴,\ r和\ r \ n。 Unicode定義了一個(gè)明確的段落分隔符矩肩,U + 2029(Cocoa為其提供常量NSParagraphSeparatorCharacter)现恼,以及一個(gè)明確的行分隔符U + 2028(Cocoa為其提供常量NSLineSeparatorCharacter)。
  • 在Cocoa文本系統(tǒng)中黍檩,NSParagraphSeparatorCharacter被一致地視為段落述暂,并且NSLineSeparatorCharacter被一致地視為不是段落中斷的換行符,即段落中的換行符建炫。 但是,在其他情況下疼蛾,幾乎不能保證如何處理這些字符肛跌。 例如,POSIX級(jí)軟件通常只識(shí)別\ n作為中斷察郁。 某些較舊的Macintosh軟件僅識(shí)別\ r \ n衍慎,某些Windows軟件僅識(shí)別\ r \ n。 通常皮钠,行和段落之間沒有區(qū)別稳捆。

In the Cocoa text system, the NSParagraphSeparatorCharacter is treated consistently as a paragraph break, and NSLineSeparatorCharacter is treated consistently as a line break that is not a paragraph break—that is, a line break within a paragraph. However, in other contexts, there are few guarantees as to how these characters will be treated. POSIX-level software, for example, often recognizes only \n as a break. Some older Macintosh software recognizes only \r, and some Windows software recognizes only \r\n. Often there is no distinction between line and paragraph breaks.

  • 在Cocoa文本系統(tǒng)中,NSParagraphSeparatorCharacter被一致地視為段落麦轰,并且NSLineSeparatorCharacter被一致地視為不是段落中斷的換行符乔夯,即段落中的換行符。 但是款侵,在其他情況下末荐,幾乎不能保證如何處理這些字符。 例如新锈,POSIX級(jí)軟件通常只識(shí)別\ n作為中斷甲脏。 某些較舊的Macintosh軟件僅識(shí)別\ r \ n,某些Windows軟件僅識(shí)別\ r \ n。 通常块请,行和段落之間沒有區(qū)別娜氏。

Which line or paragraph break character you should use depends on how your data may be used and on what platforms. The Cocoa text system recognizes \n, \r, or \r\n all as paragraph breaks—equivalent to NSParagraphSeparatorCharacter. When it inserts paragraph breaks, for example with insertNewline:, it uses \n. Ordinarily NSLineSeparatorCharacter is used only for breaks that are specifically line breaks and not paragraph breaks, for example in insertLineBreak:, or for representing HTML <br> elements.

  • 您應(yīng)該使用哪個(gè)行或段落中斷字符取決于您的數(shù)據(jù)的使用方式以及在哪些平臺(tái)上。 Cocoa文本系統(tǒng)將\ n墩新,\ n或\ r \ n全部識(shí)別為段落符號(hào) - 相當(dāng)于NSParagraphSeparatorCharacter贸弥。 當(dāng)它插入段落符號(hào)時(shí),例如insertNewline:抖棘,它使用\ n茂腥。 通常,NSLineSeparatorCharacter僅用于特定換行符而不是段落符的斷點(diǎn)切省,例如在insertLineBreak:中最岗,或用于表示HTML
    元素。

If your breaks are specifically intended as line breaks and not paragraph breaks, then you should typically use NSLineSeparatorCharacter. Otherwise, you may use \n, \r, or \r\n depending on what other software is likely to process your text. The default choice for Cocoa is usually \n.

  • 如果您的休息時(shí)間是專門用作換行符而不是分段符朝捆,那么您通常應(yīng)該使用NSLineSeparatorCharacter般渡。 否則,您可以使用\ n芙盘,\ r或\ r \ n取決于其他軟件可能處理您的文本驯用。 Cocoa的默認(rèn)選擇通常是\ n。

Separating a String “by Paragraph”

  • 按段落分隔字符串

A common approach to separating a string “by paragraph” is simply to use:

  • “按段落”分隔字符串的常用方法是使用:
NSArray *arr = [myString componentsSeparatedByString:@"\n"];

This, however, ignores the fact that there are a number of other ways in which a paragraph or line break may be represented in a string—\r, \r\n, or Unicode separators.

  • 但是儒老,這忽略了一個(gè)事實(shí)蝴乔,即在字符串 - “\ r \ n”,“\ r \ n”或Unicode分隔符中可以表示段落或換行符驮樊。

Instead you can use methods—such as enumerateSubstringsInRange:options:usingBlock: and enumerateLinesUsingBlock:—that take into account the variety of possible line terminations, as illustrated in the following example.

  • 相反薇正,您可以使用諸如enumerateSubstringsInRange:options:usingBlock:和enumerateLinesUsingBlock之類的方法: - 它考慮了各種可能的行終止,如以下示例所示囚衔。
NSString *string = /* assume this exists */;
NSRange range = NSMakeRange(0, string.length);
[string enumerateSubstringsInRange:range
                           options:NSStringEnumerationByParagraphs
                        usingBlock:^(NSString * _Nullable paragraph, NSRange paragraphRange, NSRange enclosingRange, BOOL * _Nonnull stop) {             // ... }];
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末挖腰,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子练湿,更是在濱河造成了極大的恐慌猴仑,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件肥哎,死亡現(xiàn)場(chǎng)離奇詭異辽俗,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)篡诽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門榆苞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人霞捡,你說我怎么就攤上這事坐漏。” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵赊琳,是天一觀的道長街夭。 經(jīng)常有香客問我,道長躏筏,這世上最難降的妖魔是什么板丽? 我笑而不...
    開封第一講書人閱讀 58,868評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮趁尼,結(jié)果婚禮上埃碱,老公的妹妹穿的比我還像新娘。我一直安慰自己酥泞,他們只是感情好砚殿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,892評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著芝囤,像睡著了一般似炎。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上悯姊,一...
    開封第一講書人閱讀 51,692評(píng)論 1 305
  • 那天羡藐,我揣著相機(jī)與錄音,去河邊找鬼悯许。 笑死仆嗦,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的先壕。 我是一名探鬼主播瘩扼,決...
    沈念sama閱讀 40,416評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼启上!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起店印,我...
    開封第一講書人閱讀 39,326評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤冈在,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后按摘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體包券,經(jīng)...
    沈念sama閱讀 45,782評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,957評(píng)論 3 337
  • 正文 我和宋清朗相戀三年炫贤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了溅固。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,102評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡兰珍,死狀恐怖侍郭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤亮元,帶...
    沈念sama閱讀 35,790評(píng)論 5 346
  • 正文 年R本政府宣布猛计,位于F島的核電站,受9級(jí)特大地震影響爆捞,放射性物質(zhì)發(fā)生泄漏奉瘤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,442評(píng)論 3 331
  • 文/蒙蒙 一煮甥、第九天 我趴在偏房一處隱蔽的房頂上張望盗温。 院中可真熱鬧,春花似錦成肘、人聲如沸卖局。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽吼驶。三九已至,卻和暖如春店煞,著一層夾襖步出監(jiān)牢的瞬間蟹演,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評(píng)論 1 272
  • 我被黑心中介騙來泰國打工顷蟀, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留酒请,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,332評(píng)論 3 373
  • 正文 我出身青樓鸣个,卻偏偏與公主長得像羞反,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子囤萤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,044評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,511評(píng)論 0 23
  • 本周總結(jié) 52th week涛舍,2017 本周開始看了索羅斯的《金融煉金術(shù)》澄惊,早都想看了,可是不敢企及富雅,果真還是看不...
    cc08閱讀 142評(píng)論 0 0
  • 五月24日高端酒會(huì)掸驱,西安高端酒會(huì)
    靜靜地修行閱讀 74評(píng)論 0 0
  • 小雪過后毕贼,又到了蘆花飄飛的季節(jié)了。 從東洼的臺(tái)子上割上幾根大葦蛤奢,扒皮凈去根節(jié)鬼癣,用細(xì)麻繩捆扎成六角型的“麻掛”(風(fēng)箏...
    劉秉均閱讀 437評(píng)論 0 3