Tuesday, May 14, 2013

NSString getParagraphStart end contentsEnd forRange example ios


[NSString getParagraphStart end contentsEnd forRange]

Returns by reference the beginning of the first paragraph and the end of the last paragraph touched by the given range.
- (void)getParagraphStart:(NSUInteger *)startIndex end:(NSUInteger *)endIndex contentsEnd:(NSUInteger *)contentsEndIndex forRange:(NSRange)aRange
Parameters of [NSString getParagraphStart end contentsEnd forRange]
startIndex
Upon return, contains the index of the first character of the paragraph containing the beginning of aRange. PassNULL if you do not need this value (in which case the work to compute the value isn’t performed).
endIndex
Upon return, contains the index of the first character past the terminator of the paragraph containing the end ofaRange. Pass NULL if you do not need this value (in which case the work to compute the value isn’t performed). [NSString getParagraphStart end contentsEnd forRange]
contentsEndIndex
Upon return, contains the index of the first character of the terminator of the paragraph containing the end ofaRange. Pass NULL if you do not need this value (in which case the work to compute the value isn’t performed).
aRange
A range within the receiver. The value must not exceed the bounds of the receiver.
Discussion of [NSString getParagraphStart end contentsEnd forRange]
If aRange is contained with a single paragraph, of course, the returned indexes all belong to that paragraph. Similar to getLineStart:end:contentsEnd:forRange:, you can use the results of this method to construct the ranges for paragraphs.
Example of [NSString getParagraphStart end contentsEnd forRange]
unsigned length = [string length];
unsigned paraStart = 0, paraEnd = 0, contentsEnd = 0;
NSMutableArray *array = [NSMutableArray array];
NSRange currentRange;
while (paraEnd < length)
{
    [string getParagraphStart:&paraStart end:&paraEnd
    contentsEnd:&contentsEnd forRange:NSMakeRange(paraEnd, 0)];
    currentRange = NSMakeRange(paraStart, contentsEnd - paraStart);
    [array addObject:[string substringWithRange:currentRange]];
}
Example of [NSString getParagraphStart end contentsEnd forRange]
NSUInteger paraStart, paraEnd, contEnd;
NSString *aString1 = @"Apple\u2028Orange\u2029Banana\r\nLemon";
[aString1 getParagraphStart:&paraStart end:&paraEnd contentsEnd:&contEnd forRange:NSMakeRange(14, 1)] ;
NSLog(@"%@", [NSString stringWithFormat:@"ParagraphStart=%d, ParagraphEnd=%d, ContentsEnd=%d", paraStart, paraEnd, contEnd]);

Example of [NSString getParagraphStart end contentsEnd forRange]
 NSString *string = /* assume this exists */;
    unsigned length = [string length];
    unsigned paraStart = 0, paraEnd = 0, contentsEnd = 0;
    NSMutableArray *array = [NSMutableArray array];
    NSRange currentRange;
    while (paraEnd < length) {
        [string getParagraphStart:&paraStart end:&paraEnd
                      contentsEnd:&contentsEnd forRange:NSMakeRange(paraEnd, 0)];
        currentRange = NSMakeRange(paraStart, contentsEnd - paraStart);
        [array addObject:[string substringWithRange:currentRange]];
    }