Applescript and “starts with” operator

China☆狼群 提交于 2019-12-11 02:10:01

问题


Is there a way to check (in applescript) if a list (or block of html text) starts with any number of values.

Example (checking for a single value)

if {foobar starts with "<p>"} then
    -- do something awesome here
end if

except i would like to pass multiple values to check <p> or <h1> or <em>.

Thanks in advance.


回答1:


on startswith(txt, l)
    repeat with v in l
        if txt starts with v then return true
    end repeat
    false
end startswith

startswith("abc", {"a", "d", "e"}) -- true



回答2:


If you want to stay within the 'English' style of AppleScript, although longer than the example above, you can just do this:

if {foobar starts with "hello" or foobar starts with "goodbye"} then

A full example would be:

set foobar to "hello dude"
if {foobar starts with "hello" or foobar starts with "goodbye"} then
    display dialog "found"
end if

That will be true even if you change:

set foobar to "hello dude"

to:

set foobar to "goodbye dude"


来源:https://stackoverflow.com/questions/5925439/applescript-and-starts-with-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!