从 NSTextView 中提取第一个非空白行的最有效方法?

发布于 2024-11-15 17:45:39 字数 1105 浏览 0 评论 0原文

从 NSTextView 中提取第一个非空白行的最有效方法是什么?

例如,如果文本是:

\n
\n
    \n
         This is the text I want     \n
 \n
Foo bar  \n
\n

结果将是“这是我想要的文本”。

这就是我所拥有的:

NSString *content = self.textView.textStorage.string;
NSInteger len = [content length];
NSInteger i = 0;

// Scan past leading whitespace and newlines
while (i < len && [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:[content characterAtIndex:i]]) {
    i++;
}
// Now, scan to first newline
while (i < len && ![[NSCharacterSet newlineCharacterSet] characterIsMember:[content characterAtIndex:i]]) {
    i++;
}
// Grab the substring up to that newline
NSString *resultWithWhitespace = [content substringToIndex:i];
// Trim leading and trailing whitespace/newlines from the substring
NSString *result = [resultWithWhitespace stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

是否有更好、更有效的方法?

我正在考虑将其放入 -textStorageDidProcessEditing: NSTextStorageDelegate 方法中,以便我可以在编辑文本时获取它。这就是为什么我希望该方法尽可能高效。

What is the most efficient way to pull the first non-whitespace line from an NSTextView?

For example, if the text is:

\n
\n
    \n
         This is the text I want     \n
 \n
Foo bar  \n
\n

The result would be "This is the text I want".

Here is what I have:

NSString *content = self.textView.textStorage.string;
NSInteger len = [content length];
NSInteger i = 0;

// Scan past leading whitespace and newlines
while (i < len && [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:[content characterAtIndex:i]]) {
    i++;
}
// Now, scan to first newline
while (i < len && ![[NSCharacterSet newlineCharacterSet] characterIsMember:[content characterAtIndex:i]]) {
    i++;
}
// Grab the substring up to that newline
NSString *resultWithWhitespace = [content substringToIndex:i];
// Trim leading and trailing whitespace/newlines from the substring
NSString *result = [resultWithWhitespace stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Is there a better, more efficient way?

I'm thinking of putting this in the -textStorageDidProcessEditing: NSTextStorageDelegate method so I can get it as the text is edited. That's why I'd like the method to be as efficient as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2024-11-22 17:45:39

只需使用专为此类事情设计的 NSScanner 即可:

NSString* output = nil;
NSScanner* scanner = [NSScanner scannerWithString:yourString];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] intoString:NULL];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&output];
output = [output stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

请注意,如果您可以扫描特定字符而不是字符集,速度会快得多:

[scanner scanUpToString:@"\n" intoString:&output];

Just use NSScanner which is designed for this sort of thing:

NSString* output = nil;
NSScanner* scanner = [NSScanner scannerWithString:yourString];
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] intoString:NULL];
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&output];
output = [output stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Note that it's much faster if you can scan up to a particular character rather than a character set:

[scanner scanUpToString:@"\n" intoString:&output];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文