检查字符串是否以Emacs Lisp中的后缀结尾 [英] Check if a string ends with a suffix in Emacs Lisp

查看:186
本文介绍了检查字符串是否以Emacs Lisp中的后缀结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个函数检查字符串是否以某个子字符串结尾? Python具有 endswith

 >>> 胜利.endswith(tory)
True


解决方案

只需安装 s.el 字符串操作库,并使用其 s-suffix? 谓词:

 (s-后缀?转过来。 =>但是如果你拒绝使用这个库,你必须自己编写你自己的函数。   string-prefix-p  ,这是Python的 subr.el ,它只是一个包装器 比较串 。根据 Emacs 24.3更改日志

  **新功能`string-prefix-p'。 
(这实际上是在Emacs 23.2中添加的,但是当时没有公布。)

string-suffix-p was ,所以对于早期版本,我写道:

 (defun string-suffix-p(str1 str2& optional ignore-case)
(let((begin2( - (length str2)(length str1)))
(end2(length str2)))
(when(< begin2 0)(setq begin2 0))
(eq t(compare-strings str1 nil nil
str2 begin2 end2
ignore-case))))

(when(< ; begin2 0)(setq begin2 0))是一种解决方法,因为如果将负数传递给 compare-strings ,那么barfs与 *** Eval error ***错误的类型参数:wholenump,-1



分享功能,它比 yves Baumes 解决方案更快,即使 string-match 是一个C函数。

  ELISP> (setq str1miss
str2Ayo,这里的教训,贝,你来到国王,你最好不要错过。)
ELISP> (基准运行1000000(string-suffix-p str1 str2))
(4.697675135000001 31 2.789847821000066)
ELISP> (byte-compile'string-suffix-p)
ELISP> (基准运行1000000(string-suffix-p str1 str2))
(0.43636462600000003 0 0.0)
ELISP> (基准运行1000000(字符串匹配miss\。$str2))
(1.3447664240000001 0 0.0)


Is there a function that checks that a string ends with a certain substring? Python has endswith:

>>> "victory".endswith("tory")
True

解决方案

Just install s.el string manipulation library and use its s-suffix? predicate:

(s-suffix? "turn." "...when it ain't your turn.") ; => t

But if you refuse you to use this library, you have to write your own function. There is string-prefix-p, which is an analog to Python's str.startswith, in subr.el, and it is just a wrapper around compare-strings. According to Emacs 24.3 changelog:

** New function `string-prefix-p'.
(This was actually added in Emacs 23.2 but was not advertised at the time.)

string-suffix-p was added only in Emacs 24.4, so for earlier versions i wrote:

(defun string-suffix-p (str1 str2 &optional ignore-case)
  (let ((begin2 (- (length str2) (length str1)))
        (end2 (length str2)))
    (when (< begin2 0) (setq begin2 0))
    (eq t (compare-strings str1 nil nil
                           str2 begin2 end2
                           ignore-case))))

(when (< begin2 0) (setq begin2 0)) is a workaround, because if you pass negative numbers to compare-strings, it barfs with *** Eval error *** Wrong type argument: wholenump, -1.

If you byte compile the function, it works faster than yves Baumes solution, even though string-match is a C function.

ELISP> (setq str1 "miss."
             str2 "Ayo, lesson here, Bey. You come at the king, you best not miss.")
ELISP> (benchmark-run 1000000 (string-suffix-p str1 str2))
(4.697675135000001 31 2.789847821000066)
ELISP> (byte-compile 'string-suffix-p)
ELISP> (benchmark-run 1000000 (string-suffix-p str1 str2))
(0.43636462600000003 0 0.0)
ELISP> (benchmark-run 1000000 (string-match "miss\.$" str2))
(1.3447664240000001 0 0.0)

这篇关于检查字符串是否以Emacs Lisp中的后缀结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆