[NSString getLineStart end contentsEnd forRange]
Returns by reference the beginning of the first line and the end of the last line touched by the given range.
- (void)getLineStart:(NSUInteger *)startIndex end:(NSUInteger *)lineEndIndex contentsEnd:(NSUInteger *)contentsEndIndex forRange:(NSRange)aRange
Parameters
- startIndex
- 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). [NSString getLineStart end contentsEnd forRange] - lineEndIndex
- 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). - contentsEndIndex
- 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). [NSString getLineStart end contentsEnd forRange] - aRange
- A range within the receiver. The value must not exceed the bounds of the receiver.
Discussion of [NSString getLineStart end contentsEnd forRange]
A line is delimited by any of these characters, the longest possible sequence being preferred to any shorter:
U+000D
(\r
orCR
)U+2028
(Unicode line separator)U+000A
(\n
orLF
)U+2029
(Unicode paragraph separator)\r\n
, in that order (also known asCRLF
)
If aRange is contained with a single line, of course, the returned indexes all belong to that line. You can use the results of this method to construct ranges for lines by using the start index as the range’s location and the difference between the end index and the start index as the range’s length. [NSString getLineStart end contentsEnd forRange]
This method detects all invalid ranges (including those with negative lengths). For applications linked against OS X v10.6 and later, this error causes an exception; for applications linked against earlier releases, this error causes a warning, which is displayed just once per application execution.
Example of [NSString getLineStart end contentsEnd forRange]
while (lineEndIndex < stringLength)
{
// Include only a single character in range. Not sure whether
// this will work with empty lines, but if not, try a length of 0.
range = NSMakeRange(lineEndIndex, 1);
[string getLineStart:&startIndex end:&lineEndIndex
contentsEnd:&contentsEndIndex forRange:range];
// If you want to exclude line terminators...
[lines addObject:[string
substringWithRange:NSMakeRange(startIndex, contentsEndIndex -
startIndex)]];
}
{
// Include only a single character in range. Not sure whether
// this will work with empty lines, but if not, try a length of 0.
range = NSMakeRange(lineEndIndex, 1);
[string getLineStart:&startIndex end:&lineEndIndex
contentsEnd:&contentsEndIndex forRange:range];
// If you want to exclude line terminators...
[lines addObject:[string
substringWithRange:NSMakeRange(startIndex, contentsEndIndex -
startIndex)]];
}
Example of [NSString getLineStart end contentsEnd forRange]
unsigned startIndex;
unsigned lineEndIndex;
unsigned contentsEndIndex;
NSRange range = NSMakeRange(49, 3);
[string getLineStart:&startIndex end:&lineEndIndex
contentsEnd:&contentsEndIndex forRange:range];
unsigned lineEndIndex;
unsigned contentsEndIndex;
NSRange range = NSMakeRange(49, 3);
[string getLineStart:&startIndex end:&lineEndIndex
contentsEnd:&contentsEndIndex forRange:range];
Example of [NSString getLineStart end contentsEnd forRange]