Gabriel Fair
Gabriel Fair

Reputation: 4274

Netlogo: Is there a way to compare strings insensitive to capitalization?

I could not find a way to compare two strings case-insensitive. How could I do this in netlogo?

Upvotes: 0

Views: 323

Answers (1)

Jasper
Jasper

Reputation: 2790

I wasn't able to find a purpose-built primitive to do this, or even a to-upper prim that would make it relatively easy. You can make a crude utility procedure to do it:

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

Then just equal-ignore-case? "hello" "HELLO" to compare.

This obviously doesn't work if you care about characters with accents and such. I make no guarantees for performance, either.

Upvotes: 2

Related Questions