【问题标题】:How do I get rid of specific parts of a string in AppleScript?如何摆脱 AppleScript 中字符串的特定部分?
【发布时间】:2020-11-14 21:58:19
【问题描述】:

这里是有问题的字符串:

xxx:parentdir:childdir:file.fl

我想删除从第一个冒号“:”开始直到字符串结尾的所有字符,所以我只剩下“xxx”

xxx 的字符数取决于我的输入。

这是一个别名路径,我只想要它的开头部分。

【问题讨论】:

    标签: macos path scripting applescript


    【解决方案1】:

    AppleScript 字符串中的字符具有相对于字符串的开始 (1) 或结束 (-1) 的索引或偏移量,并且可以使用具有起始和结束索引的范围来指定文本。对于您的示例,您可以搜索所需字符或文本的偏移量,并将其用作结束位置:

    set testingText to (choose file) as text
    set here to (offset of ":" in testingText) - 1 -- don't include the character
    display dialog text 1 thru here of testingText
    

    【讨论】:

      【解决方案2】:

      你可以使用AppleScript's text item delimiters来做到这一点:

      set foo to "xxx:parentdir:childdir:file.fl"
      set AppleScript's text item delimiters to ":"
      set foo to first text item of foo as string
      set AppleScript's text item delimiters to ""
      
      return foo
      

      结果:“xxx”

      【讨论】:

        猜你喜欢
        • 2020-07-24
        • 1970-01-01
        • 2018-07-27
        • 2012-11-19
        • 2018-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-27
        相关资源
        最近更新 更多