String Subst

Function Syntax (LM:StringSubst <new> <old> <str>)
Current Version 1.0
Symbol Type Description
new String String to be substituted for 'old'
old String String to be replaced
str String String to be searched
Type Description
String String with all occurrences of 'old' replaced with 'new'

Program Description

This subfunction will substitute all occurrences of a string for another string within a string.

The functionality is similar to the Visual LISP vl-string-subst (and indeed uses this function), however, this subfunction performs a global replacement, hence all occurrences of the pattern string are replaced.

Select all
;;--------------------=={ String Subst }==--------------------;;
;;                                                            ;;
;;  Substitutes a string for all occurrences of another       ;;
;;  string within a string.                                   ;;
;;  Author: Lee Mac, Copyright  2011 -       ;;
;;  Arguments:                                                ;;
;;  new - string to be substituted for 'old'                  ;;
;;  old - string to be replaced                               ;;
;;  str - the string to be searched                           ;;
;;  Returns:  String with 'old' replaced with 'new'           ;;

(defun LM:StringSubst ( new old str / inc len )
    (setq len (strlen new)
          inc 0
    (while (setq inc (vl-string-search old str inc))
        (setq str (vl-string-subst new old str inc)
              inc (+ inc len)

Example Function Call

_$ (LM:StringSubst "lisp" "be" "To be or not to be")

"To lisp or not to lisp"


increase · reset · decrease

Designed & Created by Lee Mac © 2010