版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.05.26 |
前言
前面我簡單的寫了些NSString的初始化,寫了幾篇,都不難,但是可以對新手有一定的小幫助,對于大神級人物可以略過這幾篇,NSString本來就沒有難的,都是細枝末節,忘記了查一下就會了,沒有技術難點,下面我們繼續~~~
1. NSString簡單細說(一)—— NSString整體架構
2. NSString簡單細說(二)—— NSString的初始化
3. NSString簡單細說(三)—— NSString初始化
4. NSString簡單細說(四)—— 從URL初始化
5. NSString簡單細說(五)—— 向文件或者URL寫入
6. NSString簡單細說(六)—— 字符串的長度
7. NSString簡單細說(七)—— 與C字符串的轉化
8. NSString簡單細說(八)—— 識別和比較字符串
9. NSString簡單細說(九)—— 字符串的合并
10. NSString簡單細說(十)—— 字符串的分解
11. NSString簡單細說(十一)—— 字符串的查找
12. NSString簡單細說(十二)—— 字符串的替換
字符串的分行和分段
一、- (void)getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;
我們先看一下這個參數。
方法參數
看代碼
/**
* 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]);
看結果
2017-05-26 00:23:27.629 NSString你會用嗎?[2599:87084] startPtr=0, lineEndPtr=45, contentsEndPtr=45
結論:指定行取字符串。
二、- (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);
看輸出結果
2017-05-26 00:52:08.553 NSString你會用嗎?[3209:113153] 0--45
結論:返回字符串指定行的位置和長度。
三、- (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;
先看一下參數
方法參數
直接看代碼
/**
* 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:¶Start end:¶End contentsEnd:&contEnd forRange:NSMakeRange(10, 1)] ;
NSLog(@"%@", [NSString stringWithFormat:@"ParagraphStart=%ld, ParagraphEnd=%ld, ContentsEnd=%ld", paraStart, paraEnd, contEnd]);
看結果
2017-05-26 01:04:57.889 NSString你會用嗎?[3438:123367] ParagraphStart=0, ParagraphEnd=13, ContentsEnd=12
結論:指定段分段取字符串。
四、- (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);
看結果
2017-05-26 01:14:49.067 NSString你會用嗎?[3600:132337] 0--8
結論:指定段分段的位置和長度。
后記
未完,待續~~~
風景