Objective C 中的 AppleScript 文本分隔符

发布于 2024-09-13 02:19:34 字数 656 浏览 4 评论 0原文

是否有文本分隔符的客观 C 版本?

这是 applescript 代码:

set oldDelim to AppleScript's text item delimiters

set AppleScript's text item delimiters to "Jarvis>"
set charliePlainOutput to (what ever needs to be parsed)
set charlieUnmoddedOutput to last text item of charliePlainOutput
set charlieOutputFiltered to text item 1 of charlieUnmoddedOutput
set AppleScript's text item delimiters to "[Jarvis]"
set charlieOutputLast to text item 1 of charlieOutputFiltered
set AppleScript's text item delimiters to "["
set charlieOutput to text item 1 of charlieOutputLast

set AppleScript's text item delimiters to oldDelim

有什么想法吗?

Is there an objective c version of text delimiters??

Here's the applescript code:

set oldDelim to AppleScript's text item delimiters

set AppleScript's text item delimiters to "Jarvis>"
set charliePlainOutput to (what ever needs to be parsed)
set charlieUnmoddedOutput to last text item of charliePlainOutput
set charlieOutputFiltered to text item 1 of charlieUnmoddedOutput
set AppleScript's text item delimiters to "[Jarvis]"
set charlieOutputLast to text item 1 of charlieOutputFiltered
set AppleScript's text item delimiters to "["
set charlieOutput to text item 1 of charlieOutputLast

set AppleScript's text item delimiters to oldDelim

Any ideas?

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

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

发布评论

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

评论(1

逆光下的微笑 2024-09-20 02:19:34

NSString 方法 componentsSeparatedByString: 的作用与 Applescript 中的 text items 基本相同。您可以调用than,然后访问数组中的对象以获取子字符串。此示例抓取最后一次出现“Jarvis>”之后的所有文本在原始字符串中:

NSString* originalString; //comes from somewhere
NSArray* substrings = [originalString componentsSeparatedByString:@"Jarvis>"];
NSString* lastSubstring = [substrings lastObject];

但是,如果您只想从现有字符串的前面或后面抓取子字符串,请查看 rangeOfString:, substringToIndex:,和substringFromIndex:。您将首先使用 rangeOfString: 查找要分隔的字符串,然后使用其中一个子字符串方法来获取所需的实际文本。如果您只想访问单个子字符串,而不是由文本分隔符分隔的每个子字符串,那么这比 componentsSeparatedByString: 更有效。此示例抓取原始字符串中第一次出现“[Jarvis]”之前的所有文本:

NSRange jarvisRange = [originalString rangeOfString:@"[Jarvis]"];
NSString* substring = [originalString substringToIndex:jarvisRange.location];

您还可以使用 rangeOfString:options:NSBackwardsSearch 传递给 options 参数如果您想从字符串的末尾而不是前面进行搜索。

The NSString method componentsSeparatedByString: will do basically the same thing as text items in Applescript. You can call than, then access objects from the array to get the substrings. This example grabs all the text after the last occurrence of "Jarvis>" in the original string:

NSString* originalString; //comes from somewhere
NSArray* substrings = [originalString componentsSeparatedByString:@"Jarvis>"];
NSString* lastSubstring = [substrings lastObject];

However, if you just want to grab a substring off the front or the back of an existing string, take a look at rangeOfString:, substringToIndex:, and substringFromIndex:. You would first use rangeOfString: to find the string you want to separate by, then one of the substring methods to grab the actual text you want. This is somewhat more efficient that componentsSeparatedByString: if you only want to access a single substring, rather than every single substring separated by your text delimiter. This example grabs all the text before the first occurrence of "[Jarvis]" in the original string:

NSRange jarvisRange = [originalString rangeOfString:@"[Jarvis]"];
NSString* substring = [originalString substringToIndex:jarvisRange.location];

You can also use rangeOfString:options: passing in NSBackwardsSearch to the options parameter if you want to search from the end of the string rather than the front.

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