DuanJinHui Posted July 8, 2015 Posted July 8, 2015 (edited) Hello friend. How to write this function ? (defun ins-str (str start ins-str /a b ) ....... _$ (ins-str "af127DB" 3 "AB") afAB127DB Thanks for any help ! Edited July 8, 2015 by DuanJinHui Quote
DuanJinHui Posted July 8, 2015 Author Posted July 8, 2015 Here is a example (defun tes (lst pos mod / qlst a hlst) (setq a -1) (setq hlst (vl-member-if-not '(lambda(x) (setq a (1+ a)) (if (= a pos) nil (setq qlst (cons x qlst)) ) ) lst ) ) (if mod (apply 'append (list (reverse(cons mod qlst)) hlst)) (apply 'append (list (reverse qlst) (cdr hlst)) ) ) ) _$(tes '(1 2 3 4 5 6) 2 0) ->(1 2 0 3 4 5 6) _$(tes '(1 2 3 4 5 6) 2 nil) ->(1 2 4 5 6) But this function is only for list , not for string . and (lst pos mod / qlst a hlst) "mod " must be number, can't be letter. what can I do ? Quote
tombu Posted July 8, 2015 Posted July 8, 2015 (defun ins-str (str start ins-str)(strcat(substr str 1 (- start 1)) "AB" (substr str start))) Then (ins-str "af127DB" 3 "AB") returns "afAB127DB" Quote
DuanJinHui Posted July 8, 2015 Author Posted July 8, 2015 (defun ins-str (str start ins-str)(strcat(substr str 1 (- start 1)) "AB" (substr str start))) Then (ins-str "af127DB" 3 "AB") returns "afAB127DB" Hi tombu , Thank you In function ,"AB" must replace by ins-str (defun ins-str (str start ins-str) (strcat(substr str 1 (- start 1)) ins-str (substr str start)) ) Quote
tombu Posted July 8, 2015 Posted July 8, 2015 Hi tombu , Thank youIn function ,"AB" must replace by ins-str Oops! Missed that, good to see you finished that off yourself. Happy Coding! Quote
DuanJinHui Posted July 8, 2015 Author Posted July 8, 2015 Oops! Missed that, good to see you finished that off yourself. Happy Coding! Thank you . tombu . I have another question. How extract string by a list ? eg. string "ABCD478EFJK" , list (2 5 6 8 ) returns "B47E" Quote
tombu Posted July 8, 2015 Posted July 8, 2015 (defun str-lst (str lst / slst) (setq slst "") (foreach n '(2 5 6 (setq slst (strcat slst (substr str n 1)))) ) Then (str-lst "ABCD478EFJK" '(2 5 6 ) returns "B47E" Quote
Tharwat Posted July 8, 2015 Posted July 8, 2015 My attempt (vl-list->string (mapcar '(lambda (i) (nth (1- i) (vl-string->list "ABCD478EFJK"))) '(2 5 6 )) Quote
tombu Posted July 8, 2015 Posted July 8, 2015 Oops again, forgot to use lst in the function. (defun str-lst (str lst / slst)(setq slst "")(foreach n lst(setq slst (strcat slst (substr str n 1))))) or Tharwat's (defun str-lst (str lst / slst)(vl-list->string (mapcar '(lambda (i) (nth (1- i) (vl-string->list str))) lst))) Quote
DuanJinHui Posted July 9, 2015 Author Posted July 9, 2015 Oops again, forgot to use lst in the function.(defun str-lst (str lst / slst)(setq slst "")(foreach n lst(setq slst (strcat slst (substr str n 1))))) or Tharwat's (defun str-lst (str lst / slst)(vl-list->string (mapcar '(lambda (i) (nth (1- i) (vl-string->list str))) lst))) Thank you Tharwat , and Thank you tombu . Hi tombu . Tharwat's should be like this: (defun str-lst (str lst ) (vl-list->string (mapcar '(lambda (i) (nth (1- i) (vl-string->list str))) lst)) ) Need remove Local variables Quote
Lee Mac Posted July 9, 2015 Posted July 9, 2015 If using the mapcar approach, I would suggest assigning the list of ASCII character codes representing the string to a local variable, else the string is being needlessly converted for every index in the supplied list: (defun str-lst ( str lst / tmp ) (setq tmp (vl-string->list str)) (vl-list->string (mapcar '(lambda ( n ) (nth (1- n) tmp)) lst)) ) Quote
DuanJinHui Posted July 9, 2015 Author Posted July 9, 2015 If using the mapcar approach, I would suggest assigning the list of ASCII character codes representing the string to a local variable, else the string is being needlessly converted for every index in the supplied list: (defun str-lst ( str lst / tmp ) (setq tmp (vl-string->list str)) (vl-list->string (mapcar '(lambda ( n ) (nth (1- n) tmp)) lst)) ) Lee ,Thank you for your advice . Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.