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
    Commented May 17, 2015 at 15:30

3 Answers 3


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.


Another short alternative:

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

equalp from the deprecated cl library is another alternative. From (documentation 'cl-equalp), my emphasis in bold:

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


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
    Commented Jan 28, 2016 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
    Commented Jan 29, 2016 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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