我找不到比较不区分大小写的两个字符串的方法。如何在netlogo中做到这一点?
答案 0 :(得分:2)
我无法找到专门构建的原语来执行此操作,甚至找不到to-upper
原语来简化操作。您可以制作一个粗略的实用程序来做到这一点:
to-report equal-ignore-case? [ str1 str2 ]
if (length str1 != length str2) [ report false ]
foreach (range length str1) [ i ->
let c1 (item i str1)
let c2 (item i str2)
; if c1 = c2, no need to do the `to-upper-char` stuff
if (c1 != c2 and to-upper-char c1 != to-upper-char c2) [
report false
]
]
report true
end
; this only works with a string length 1
to-report to-upper-char [ c ]
let lower "abcdefghijklmnopqrstuvwxyz"
let upper "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
let pos (position c lower)
report ifelse-value (is-number? pos) [ item pos upper ] [ c ]
end
然后仅equal-ignore-case? "hello" "HELLO"
进行比较。
如果您关心带有重音等字符,显然这是行不通的。我也不保证性能。