我想測驗一個字串是否包含兩個文本項(“&”或“和”)中的任何一個。如果字串包含任一專案,我希望測驗回傳 True,否則回傳 False。我使用這個測驗來確定指定的字串(輸入到表單欄位中的人名)是一個人的名字還是一對夫婦的名字。
問題:這段代碼似乎在回傳 True 和 False 之間隨機搖擺不定。我已經嘗試直接測驗字串,測驗對字串的變數參考,檢查不同的字串,并在運行測驗之前明確重置測驗變數的值......但回傳的布林值仍然是不可預測的。
我錯過了什么??(如果這是 AppleScript 限制的問題,我愿意使用 JavaScript 來檢查字串)。
當測驗在變數x和y上運行時,下面的代碼應該回傳 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
uj5u.com熱心網友回復:
問題是在你使用some text item of coupleIdentifiers它隨機回傳一個專案的coupleIdentifiers。嘗試多次運行以下兩行代碼:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361589.html
上一篇:根據陣列物件資料創建一個新陣列
