目标 c - 如何在"n"字符后获取值?



我想从字符串中取新行字符n之后的所有值。如何获取这些值?

试试这个:

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

在这里查看方法componentsSeparatedByString

参考文献中的一个快速示例:

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

这将生成一个字符串分隔的NSArray{ @"Norman", @"Stanley", @"Fletcher" }

这里有一个类似的函数,它通过delimeter分割字符串,并返回带有两个修剪值的数组。

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];
}

相关内容

  • 没有找到相关文章

最新更新