开发者

How to get values after "\n" character?

开发者 https://www.devze.com 2023-03-10 13:46 出处:网络
I want to take all values after a new line character \\n from my string. How can I get those val开发者_如何学JAVAues?Try this:

I want to take all values after a new line character \n from my string. How can I get those val开发者_如何学JAVAues?


Try this:

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


Take a look at method componentsSeparatedByString here.

A quick example taken from reference:

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

this will produce a NSArray with strings separated: { @"Norman", @"Stanley", @"Fletcher" }


Here is similar function which splits the string by delimeter and return 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号