How to get the values ​​after the character "\ n"?

I want to take all the values ​​after a new line character \nfrom my line. How can I get these values?

+5
source share
3 answers

Try the following:

NSString *substring = nil;
NSRange newlineRange = [yourString rangeOfString:@"\n"];
if(newlineRange.location != NSNotFound) {
  substring = [yourString substringFromIndex:newlineRange.location];
}
+8
source

Take a look at the method componentsSeparatedByString here .

Quick example taken from the link:

NSString *list = @"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:@", "];

this will lead to creation NSArraywith highlighted lines:{ @"Norman", @"Stanley", @"Fletcher" }

+1
source

Here is a similar function that breaks a string into a delimeter and a returned array with two trimmed values.

NSArray* splitStrByDelimAndTrim(NSString *string, NSString *delim)
{
    NSRange range = [string rangeOfString: delim];

    NSString *first;
    NSString *second;

    if(range.location == NSNotFound)
    {
        first = @"";
        second = string;
    }
    else
    {
        first = [string substringToIndex: range.location];
        first = [first stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
        second = [string substringFromIndex: range.location + 1];
        second = [second stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
    }

    return [NSArray arrayWithObjects: first, second, nil];
}
0
source

All Articles