使用Applescript将字符串的结尾编号分隔为变量

时间:2013-03-24 18:41:55

标签: numbers applescript

我想知道是否可以在随机字符串的末尾检测到一个数字。例如在这个字符串中:“localfile / random / 1”我想拆分这个字符串,这样一个变量就是字符串末尾的数字,第二个变量就是路径的其余部分。我无法谷歌任何东西。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

如果你可以在最后一个斜线周围分割字符串:

set x to "localfile/random/1"
set text item delimiters to "/"
{(text items 1 thru -2 of x) as text, last text item of x}

如果字符串始终以数字结尾,则这两种方法都有效:

on test(x)
    set c to count of x
    repeat with i from 1 to c
        if item (c - i) of x is not in items of "0123456789" then
            return {text 1 thru (c - i) of x, text (c - i + 1) thru -1 of x}
        end if
    end repeat
end test
test("localfile/random/1")

do shell script "sed -E $'s/([0-9]*)$/\\\\\\n\\\\1/' <<< " & quoted form of "localfile/random/1"
set {x, y} to paragraphs of result

答案 1 :(得分:0)

这样您就可以拆分字符串,而无需在数字字符串前指定最后一个字符:

set myString to "localfile/random/13451"
set delimiter to "||"

set {TID, text item delimiters} to {text item delimiters, delimiter}
set myItems to text items of (do shell script "sed -E 's/([0-9]+$)/" & delimiter & "\\1/' <<< " & quoted form of myString)
set text item delimiters to TID
return myItems 

答案 2 :(得分:0)

这个怎么样?

set s to "blah4"
set l to (get last character of s)
if "1234567890" contains l then log "yes"