NSString簡單細(xì)說(十三)—— 字符串的分行和分段

版本記錄

版本號(hào) 時(shí)間
V1.0 2017.05.26

前言

前面我簡單的寫了些NSString的初始化,寫了幾篇,都不難,但是可以對(duì)新手有一定的小幫助,對(duì)于大神級(jí)人物可以略過這幾篇,NSString本來就沒有難的,都是細(xì)枝末節(jié),忘記了查一下就會(huì)了,沒有技術(shù)難點(diǎn),下面我們繼續(xù)~~~
1. NSString簡單細(xì)說(一)—— NSString整體架構(gòu)
2. NSString簡單細(xì)說(二)—— NSString的初始化
3. NSString簡單細(xì)說(三)—— NSString初始化
4. NSString簡單細(xì)說(四)—— 從URL初始化
5. NSString簡單細(xì)說(五)—— 向文件或者URL寫入
6. NSString簡單細(xì)說(六)—— 字符串的長度
7. NSString簡單細(xì)說(七)—— 與C字符串的轉(zhuǎn)化
8. NSString簡單細(xì)說(八)—— 識(shí)別和比較字符串
9. NSString簡單細(xì)說(九)—— 字符串的合并
10. NSString簡單細(xì)說(十)—— 字符串的分解
11. NSString簡單細(xì)說(十一)—— 字符串的查找
12. NSString簡單細(xì)說(十二)—— 字符串的替換

字符串的分行和分段

一、- (void)getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;

我們先看一下這個(gè)參數(shù)。

方法參數(shù)

看代碼

    /**
     * 1. - (void)getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;
     *
     *  @param startPtr:Upon return, contains the index of the first character of the line containing the beginning of aRange. Pass NULL if you do not need this value (in which case the work to compute the value isn’t performed).
     
     *  @param lineEndPtr:Upon return, contains the index of the first character past the terminator of the line containing the end of aRange. Pass NULL if you do not need this value (in which case the work to compute the value isn’t performed). 
     
     * @param contentsEndPtr:Upon return, contains the index of the first character of the terminator of the line containing the end of aRange. Pass NULL if you do not need this value (in which case the work to compute the value isn’t performed).
     
     *  @param range:A range within the receiver. The value must not exceed the bounds of the receiver.Raises an NSRangeException if aRange is invalid.
     *
     *  @return: Returns by reference the beginning of the first line and the end of the last line touched by the given range.
     */
    
    NSUInteger startPtr;
    NSUInteger lineEndPtr;
    NSUInteger contentsEndPtr;
    NSRange range = NSMakeRange(0, 10);
    NSString *str = @"ABCDEFRDSEYFDSAJagsvwjwiekeoqqywuwn1234567890";
    [str getLineStart:&startPtr end:&lineEndPtr contentsEnd:&contentsEndPtr forRange:range];
    NSLog(@"%@", [NSString stringWithFormat:@"startPtr=%ld, lineEndPtr=%ld, contentsEndPtr=%ld", startPtr, lineEndPtr, contentsEndPtr]);

看結(jié)果

2017-05-26 00:23:27.629 NSString你會(huì)用嗎?[2599:87084] startPtr=0, lineEndPtr=45, contentsEndPtr=45

結(jié)論:指定行取字符串。


二、- (NSRange)lineRangeForRange:(NSRange)range;

看代碼

    /**
     * 2. - (NSRange)lineRangeForRange:(NSRange)range;
     *
     *  @param range:A range within the receiver. The value must not exceed the bounds of the receiver.
     *
     *  @return: Returns the range of characters representing the line or lines containing a given range.
     */
    
    NSString *str = @"ABCDEFRDSEYFDSAJagsvwjwiekeoqqywuwn1234567890";
    NSRange range = NSMakeRange(0, 10);
    NSRange range1 = [str lineRangeForRange:range];
    NSLog(@"%ld--%ld",range1.location,range1.length);

看輸出結(jié)果

2017-05-26 00:52:08.553 NSString你會(huì)用嗎?[3209:113153] 0--45

結(jié)論:返回字符串指定行的位置和長度。


三、- (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;

先看一下參數(shù)

方法參數(shù)

直接看代碼

    /**
     * 3. - (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;
      *
      *  @param startPtr:Upon return, contains the index of the first character of the line containing the beginning of aRange. Pass NULL if you do not need this value (in which case the work to compute the value isn’t performed).
 
      *  @param parEndPtr:Upon return, contains the index of the first character past the terminator of the line containing the end of aRange. Pass NULL if you do not need this value (in which case the work to compute the value isn’t performed).
 
      *  @param contentsEndPtr:Upon return, contains the index of the first character of the terminator of the paragraph containing the end of aRange. Pass NULL if you do not need this value (in which case the work to compute the value isn’t performed).
 
      *  @param range:A range within the receiver. The value must not exceed the bounds of the receiver.Raises an NSRangeException if aRange is invalid.
      *
      *  @return: Returns by reference the beginning of the first line and the end of the last line touched by the given range.
     */
    
    NSUInteger paraStart, paraEnd, contEnd;
    NSString *aString1 = @"Apple\u2028Orange\u2029Banana\r\nLemon";
    [aString1 getParagraphStart:&paraStart end:&paraEnd contentsEnd:&contEnd forRange:NSMakeRange(10, 1)] ;
    NSLog(@"%@", [NSString stringWithFormat:@"ParagraphStart=%ld, ParagraphEnd=%ld, ContentsEnd=%ld", paraStart, paraEnd, contEnd]);

看結(jié)果

2017-05-26 01:04:57.889 NSString你會(huì)用嗎?[3438:123367] ParagraphStart=0, ParagraphEnd=13, ContentsEnd=12

結(jié)論:指定段分段取字符串。


四、- (NSRange)paragraphRangeForRange:(NSRange)range;

看代碼

    /**
     * 4. - (NSRange)paragraphRangeForRange:(NSRange)range;
     *
     *  @param range:A range within the receiver. The value must not exceed the bounds of the receiver.
     *
     *  @return: Returns the range of characters representing the line or lines containing a given range.
     */

    NSString *str = @"ABCDEF\r\nRDSEYF\r\nDSAJagsvwj\r\nwiekeoqqyw\r\nuwn1234567890";
    NSRange range = NSMakeRange(0, 1);
    NSRange range1 = [str paragraphRangeForRange:range];
    NSLog(@"%ld--%ld",range1.location,range1.length);

看結(jié)果

2017-05-26 01:14:49.067 NSString你會(huì)用嗎?[3600:132337] 0--8

結(jié)論:指定段分段的位置和長度。

后記

未完,待續(xù)~~~

風(fēng)景
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容