代码之家  ›  专栏  ›  技术社区  ›  Gabriel Fair

Netlogo:有没有办法比较对大写不敏感的字符串?

  •  0
  • Gabriel Fair  · 技术社区  · 6 年前

    我找不到一种方法来比较两个不区分大小写的字符串。我怎样才能在netlogo中做到这一点?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Jasper    6 年前

    我找不到一个专门构建的原语来做这个,甚至找不到一个 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" 比较。

    如果你关心有口音之类的角色,这显然是行不通的。我也不保证性能。