Tuesday, May 14, 2013

NSString getLineStart example ios


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).
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). [NSString getLineStart]
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).
aRange
A range within the receiver. The value must not exceed the bounds of the receiver.

Important: Raises an NSRangeException if any part of aRange lies beyond the end of the string.
Discussion of [NSString getLineStart]
A line is delimited by any of these characters, the longest possible sequence being preferred to any shorter:
  • U+000D (\r or CR)
  • U+2028 (Unicode line separator)
  • U+000A (\n or LF)
  • U+2029 (Unicode paragraph separator)
  • \r\n, in that order (also known as CRLF)
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]
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]
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)]];
}

Example of [NSString getLineStart]
unsigned startIndex;
unsigned lineEndIndex;
unsigned contentsEndIndex;
NSRange range = NSMakeRange(49, 3);

[string getLineStart:&startIndex end:&lineEndIndex
contentsEnd:&contentsEndIndex forRange:range];

Example of [NSString getLineStart]
-(NSString *)method067
{
    
    NSUInteger lineStart,lineEnd,contentsEnd;
    NSString *str = [NSString stringWithString:@"stringエnline2エnline3"] ;
    [str   getLineStart: &lineStart
                    end:  &lineEnd
            contentsEnd: &contentsEnd
               forRange: NSMakeRange(8,3)];
    NSString *str1 = [[NSString alloc] initWithFormat:@"lineStart=%ld,lineEnd=%ld,contentsEnd=%ld",lineStart,lineEnd,contentsEnd]  ;                            


    return str1;//lineStart=0,lineEnd=20,contentsEnd=20
}