8

Q: how does one test string equality but ignore case?

I've got a situation in which I'd like to compare strings but ignore case. Case is significant for string-equal, and apparently is insensitive to case-fold-search:

(string-equal "string" "StrinG")        ; => nil
(let ((case-fold-search t))
  (string-equal "string" "StrinG"))     ; => nil

I could always preprocess the strings (say, with downcase) before comparing them:

(string-equal (downcase "string")
              (downcase "StrinG"))      ; => t

But that seems like overkill to do two extra function calls every time. Have I overlooked the relevant predicate function somewhere?

  • I used compare-strings and found the following problem. (setq str "title") (cond (compare-strings str "authortitle") do something.) (compare-strings str "title") do something else.) The first compare-strings returns a positive value implying true which is not what I want. Therefore I used the downcase suggestion. – user8343 May 17 '15 at 15:30
7

You can use compare-strings:

(compare-strings STR1 START1 END1 STR2 START2 END2 &optional IGNORE-CASE)

Use nils for starts and ends to use the whole strings.

  • Thanks; I'd seen that one but didn't realize that one could use nils as you mentioned. I'll leave the question open a little longer in case there's another option that doesn't require 4 extra nils in a function call. – Dan Mar 30 '15 at 11:38
5

Use string-suffix-p:

(string-suffix-p "string" "strinG" t)

Much simpler and doesn't require 4 nils. It doesn't test if strings are equal per se ((string-suffix-p "word" "buzzword" t) ⇒ t). Although, in some cases this should be sufficient (if you have limited dictionary to check upon).

  • 2
    (string-suffix-p "strinG" "this string" t) also returns t. This is not what the OP wants. I suggest you either modify or delete this answer. – Tobias Jan 28 '16 at 22:09
  • Answer updated. I will leave it because string-{prefix,suffix}-p can useful in many ways: test if word at point is given keyboard (in SQL for example where case can be mixed) etc. – rgtk Jan 29 '16 at 15:31
3

Another short alternative:

(equalp "string" "strinG")      ; => t
(equalp "strinG" "this string") ; => nil 
(equalp "word" "buzzword")      ; => nil

equalp is short for cl-equalp; from (documentation 'equalp):

like ‘equal’, except that it accepts numerically equal numbers of different types (float vs. integer), and also compares strings case-insensitively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.