如何检查elisp字符串是否是另一个字符串的子字符串?


25

如何检查一个字符串s1是否是另一个字符串的子字符串s2

例如(test-substring "f t" "df tj") --> t(test-substring "ft" "df tj") --> nil

Answers:


36

标准的Emacs Lisp方法是正则表达式匹配:

(string-match-p (regexp-quote needle) haystack)

14

cl-search 可以做到这一点(并且如果找到的话,还返回子字符串的索引):

ELISP> (cl-search "f t" "df tj")
1 (#o1, #x1, ?\C-a)
ELISP> (cl-search "ft" "df tj")
nil

1
谢谢,这可以正确回答问题。让我等待其他解决方案。
命名
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.