macos 如何从 NSString 获取每一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/342577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:59:02  来源:igfitidea点击:

How do I get each line from an NSString?

objective-ccocoamacos

提问by Roland Rabien

If I have an NSString with a text file in it, how do I get an NSArray of NSString with each NSString containing a line of the file.

如果我有一个带有文本文件的 NSString,我如何获得 NSString 的 NSArray,其中每个 NSString 包含一行文件。

In 10.5 I did this:

在 10.5 中,我这样做了:

NSArray* lines = [str componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];

But that doesn't work in 10.4, and my program needs to work in 10.4.

但这在 10.4 中不起作用,我的程序需要在 10.4 中运行。

As well, it needs to work with \r, \n and \r\n line endings.

同样,它需要使用 \r、\n 和 \r\n 行结尾。

回答by e.James

The following code is straight from Apple's documentation regarding paragraphs and line breaks:

以下代码直接来自Apple 关于段落和换行符的文档

unsigned length = [string length];
unsigned paraStart = 0, paraEnd = 0, contentsEnd = 0;
NSMutableArray *array = [NSMutableArray array];
NSRange currentRange;
while (paraEnd < length)
{
    [string getParagraphStart:&paraStart end:&paraEnd
    contentsEnd:&contentsEnd forRange:NSMakeRange(paraEnd, 0)];
    currentRange = NSMakeRange(paraStart, contentsEnd - paraStart);
    [array addObject:[string substringWithRange:currentRange]];
}

I'm not 100% sure if it will work with 10.4

我不是 100% 确定它是否适用于 10.4

回答by Vineet Choudhary

Enumerates all the lines in a string using enumerateLinesUsingBlock:

使用枚举字符串中的所有行 enumerateLinesUsingBlock:

[yourString enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
    //line
}];

Declaration

- (void)enumerateLinesUsingBlock:(void (^)(NSString *line,
                                   BOOL *stop))block

Parameters

blockThe block executed for the enumeration.

The block takes two arguments:

lineThe current line of the string being enumerated. The line contains just the contents of the line, without the line terminators.

stopA reference to a Boolean value that the block can use to stop the enumeration by setting *stop = YES; it should not touch *stop otherwise.

AvailabilityAvailable in OS 10.6and iOS 4.0and later.

宣言

- (void)enumerateLinesUsingBlock:(void (^)(NSString *line,
                                   BOOL *stop))block

参数

block为枚举执行的块。

该块有两个参数:

line被枚举的字符串的当前行。该行仅包含该行的内容,没有行终止符。

stop对布尔值的引用,块可使用该值通过设置 *stop = YES 来停止枚举;它不应该触及 *stop 否则。

可用性可用的OS 10.6iOS 4.0以后。

回答by Heng-Cheong Leong

I'll first replace all "\r" with "\n", then replace all "\n\n" with "\n", and then do a componentsSeparatedByString:@"\n".

我会先用“\n”替换所有的“\r”,然后用“\n”替换所有的“\n\n”,然后做一个componentsSeparatedByString:@"\n"。

回答by Rich Catalano

Straight from a project of mine:

直接来自我的一个项目:

 NSString * fileContents = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:nil];
 NSMutableArray * fileLines = [[NSMutableArray alloc] initWithArray:[fileContents componentsSeparatedByString:@"\r\n"] copyItems: YES];

I'm not sure how to make it it automatically work with any type of line ending in 10.4.

我不确定如何让它自动与以 10.4 结尾的任何类型的行一起使用。

回答by John Safranek

According to the NSString class reference, the NSString message componentsSeparatedByCharactersInSet: is available in OS X 10.5 or later. You need to use componentsSeparatedByString:.

根据NSString 类参考,NSString 消息componentsSeparatedByCharactersInSet:在 OS X 10.5 或更高版本中可用。您需要使用 componentsSeparatedByString:。

回答by siburb

You can also do enumerateSubstringsInRange:with the NSStringEnumerationByLinesand/or NSStringEnumerationByParagraphsoptions.

您也可以enumerateSubstringsInRange:使用NSStringEnumerationByLines和/或NSStringEnumerationByParagraphs选项。

The benefit of doing it that way is that you get the NSRange of each substring, and can also set the initial range of the string to enumerate.

这样做的好处是可以获得每个子字符串的 NSRange,还可以设置要枚举的字符串的初始范围。

[myString enumerateSubstringsInRange:NSMakeRange(0, myString.length) options:NSStringEnumerationByLines | NSStringEnumerationByParagraphs usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {

}];