Jump to content

Recommended Posts

Posted (edited)

Sort of addslashes type method; nothing special figured it may be useful at some point to someone.

 

EDIT:

 

Yeah that was a bit off; amended code below

;;-Ollie
;
;  Precede each instance of an identifying char (single|group) within the source
;  string with a char (single|group)
;
;  text       = (String) source string
;  iinsert    = (String) text to insert in front of matches
;  identifier = (String) text to be preceded by the insert text in the source string

(defun addMidText(text insert identifier / i ids textLen idLen insertionLen)
 (setq i  0)
 (setq textLen (strlen text))
 (setq idLen(strlen identifier))
 (setq insertLen (strlen insert))
 (while(< (setq i (1+ i))(- textLen idLen))
   (if(eq (substr text i idLen) identifier)
     (progn
       (setq text
     (strcat
       (substr text 1 (1- i))
       insert
       (substr text i (- textLen (1- i)))))
   (setq i (+ i  insertLen) textLen(+ textLen insertLen ))
     )
   ) 
 )
 text
)

Cheers,

Ollie.

Edited by ollie
Posted

On first testing, I couldn't get it to function, but I may have used it incorrectly,

 

However, what about vl-string-subst?

 

Or for multiple instances:

 

(defun StringSubst ( new old str )
 ;; © Lee Mac 2010
 (
   (lambda ( i / nl ) (setq nl (strlen new))
     (while
       (and (< i (strlen str))
         (setq i (vl-string-search old str i))
         (setq str (vl-string-subst new old str i) i (+ i nl))
       )
     )
     str
   )
   0
 )
)

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