Take the 2-minute tour ×
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It's 100% free, no registration required.

How to check if a string s1 is a substring of another string s2?

For example (test-substring "f t" "df tj") --> t, (test-substring "ft" "df tj") --> nil.

share|improve this question

2 Answers 2

The standard Emacs Lisp approach is regular expression matching:

(string-match-p (regexp-quote needle) haystack)
share|improve this answer
    
Thanks, works well. –  Name 12 hours ago

cl-search can do that (and also returns the index of the substring, if found):

ELISP> (cl-search "f t" "df tj")
1 (#o1, #x1, ?\C-a)
ELISP> (cl-search "ft" "df tj")
nil
share|improve this answer
    
Thanks, this correctly answers the question. Let me wait for some other solutions. –  Name 12 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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