AppleScript中有没有类似于array.split的函数

Is there a function similar to array.split in AppleScript

因此在 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"}

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

我刚才也在搜索同样的东西,但答案并不那么简单。你必须使用 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