【问题标题】:AppleScript issue checking string for specified textAppleScript 问题检查指定文本的字符串
【发布时间】:2021-12-25 14:01:20
【问题描述】:

我想测试一个字符串是否包含两个文本项(“&”或“和”)。如果字符串包含任何一个项目,我希望测试返回 True,如果不包含,则返回 False。 我正在使用这个测试来确定指定的字符串(输入到表单字段中的人的名字)是一个人的名字还是一对夫妇的名字。

问题:此代码似乎在返回 True 和 False 之间随机摇摆。我尝试过直接测试字符串,测试对字符串的变量引用,检查不同的字符串,并在运行测试之前显式重置测试变量的值......但是返回的布尔值仍然是不可预测的。

我错过了什么? (如果这是 AppleScript 的限制问题,我愿意使用 JavaScript 来检查字符串)。

当测试在变量 xy 上运行时,下面的代码应该返回 True,但在变量 z 上运行时应该返回 False(因为“Southerland”的“and”不是一个独立的词)。

set x to "John Jacob & Johanna Smith"
set y to "Kathy and Kurt Gallows"
set z to "Barbara Southerland"
set theName to x
set coupleIdentifiers to {"&", " and "}
if theName contains some text item of coupleIdentifiers then
    set testIt to true
else
    set testIt to false
end if
get testIt

【问题讨论】:

    标签: string object applescript contains


    【解决方案1】:

    问题在于您使用some text item of coupleIdentifiers,它随机返回coupleIdentifiersitem。尝试只运行以下两行 代码 几次:

    set coupleIdentifiers to {"&", " and "}
    some text item of coupleIdentifiers
    

    运行它足够的次数,它会返回两个项目,一次一个。

    您需要使用不同的查询,例如:

    if theName contains "&" or theName contains " and " then
    

    如:

    set x to "John Jacob & Johanna Smith"
    set y to "Kathy and Kurt Gallows"
    set z to "Barbara Southerland"
    set theName to x
    
    if theName contains "&" or theName contains " and " then
        set testIt to true
    else
        set testIt to false
    end if
    
    get testIt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多