我正在寻找一种效率最高的Elisp代码,该代码可以在给定字符串中每隔n个字符插入一个给定字符。
答案 0 :(得分:0)
(defun jea-insert-character-every-n-characters (beginstr insert-char count)
(let ((blen (length beginstr))
(result '())
(c 1)
(i 0))
(while (< i blen)
(push (elt beginstr i) result)
(if (= c count)
(progn
(push insert-char result)
(setq c 0)))
(setq i (1+ i))
(setq c (1+ c)))
(concat (reverse result))))
(jea-insert-character-every-n-characters "This is the first test" ?X 3) ;; "ThiXs iXs tXhe XfirXst XtesXt"
(jea-insert-character-every-n-characters "This is the second test" ?X 1) ;; "TXhXiXsX XiXsX XtXhXeX XsXeXcXoXnXdX XtXeXsXtX"
(jea-insert-character-every-n-characters "Boop the snoot" ?z 7) ;; "Boop thze snootz"
可能有一种更有效的方法(我的elisp生锈了)。