【问题标题】:Is there a function similar to array.split in AppleScriptAppleScript中有没有类似array.split的函数
【发布时间】:2021-10-16 21:17:45
【问题描述】:

因此,在 Javascript 中,您可以按照以下方式编写一些内容:

let array = "This.should.be.an.array";
array = array.split(".");
console.log(array)

/* This,should,be,an,array */

现在我知道在 Applescript 中有:

set theText to "This should be a list"
set theList to every word of theText
return theList

{"This", "should", "be", "a", "list"}

还有:

set theText to "This
should
be
a
list"
set theList to every paragraph of theText
return theList

{"This", "should", "be", "a", "list"}

还有:

set theText to "Thisshouldbealist"
set theList to every character of theText
return theList


{"T", "h", "i", "s", "s", "h", "o", "u", "l", "d", "b", "e", "a", "l", "i", "s", "t"}

但我不知道如何拆分列表,例如单词之间的句点。

【问题讨论】:

    标签: javascript arrays applescript


    【解决方案1】:

    我不久前也在寻找同样的东西,但答案并不那么简单。你必须使用Applescript's text item delimiters。如果你想用句点分割一个字符串,你会得到这样的东西:

    set array to "This.should.be.an.array"
    set AppleScript's text item delimiters to "."
    set array to every text item of array
    set AppleScript's text item delimiters to ""
    

    或者写成一个函数,它看起来像这样:

    on split(theString, theSplitter)
        set AppleScript's text item delimiters to theSplitter
        set theString to every text item of theString
        set AppleScript's text item delimiters to ""
        return theString
    end split
    

    这是一种保留旧分隔符 posted by Erik on erikslab.com 的方法:

    on theSplit(theString, theDelimiter)
        -- save delimiters to restore old settings
        set oldDelimiters to AppleScript's text item delimiters
        -- set delimiters to delimiter to be used
        set AppleScript's text item delimiters to theDelimiter
        -- create the array
        set theArray to every text item of theString
        -- restore the old setting
        set AppleScript's text item delimiters to oldDelimiters
        -- return the result
        return theArray
    end theSplit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 2014-10-21
      • 2021-09-17
      • 2020-11-04
      相关资源
      最近更新 更多