Jump to content

Recommended Posts

Posted (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 by DuanJinHui
Posted

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 ?

Posted

(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"

Posted
(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))
)

Posted
Hi tombu , Thank you

In function ,"AB" must replace by ins-str

 

Oops! Missed that, good to see you finished that off yourself.

 

Happy Coding!

Posted
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"

Posted

(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"

Posted

My attempt :)

 

(vl-list->string (mapcar '(lambda (i) (nth (1- i) (vl-string->list "ABCD478EFJK"))) '(2 5 6 ))

Posted

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)))

Posted
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

Posted

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))
)

Posted
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 .

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...