版本记录
版本号 | 时间 |
---|---|
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
结论:指定段分段的位置和长度。
后记
未完,待续~~~