vl-string-translate question

Daron Denton

Usenet Archive
Joined
Jul 21, 2003
Messages
55
Reaction score
0
could someone try replacing "\\" with "\\\\" using vl-string-translate or vl-string-subst?

(vl-string-subst "\\" "\\\\" "g:\\test1\\test2\\")
returns = "g:\\test1\\test2\\"

(vl-string-translate "\\" "\\\\" "g:\\test1\\test2\\")
returns = "g:\\test1\\test2\\"

(dos_strreplace "g:\\test1\\test2\\" "\\" "\\\\")
returns = "g:\\\\test1\\\\test2\\\\" <<<---this is what i'm looking for

thanks,
daron
 
Daron,

the vl-string-subst substitute only the first one the function finds.

something like this:

(defun string-to-list (str pat / i j n lst)
(cond
((/= (type str) (type pat) 'STR))
((= str pat) '(""))
(T
(setq i 0
n (strlen pat))
(while (setq j (vl-string-search pat str i))
(setq lst (cons (substr str (1+ i) (- j i)) lst)
i (+ j n)))
(mapcar '(lambda (x) (vl-string-trim " " x))
(reverse (cons (substr str (1+ i)) lst))))))

(apply 'strcat
(mapcar '(lambda (str) (strcat str "\\\\"))
(vl-remove-if-not
'(lambda (str) (/= str ""))
(string-to-list "g:\\test1\\test2\\" "\\"))))

"g:\\\\test1\\\\test2\\\\"

hth.
 
thanks Luis. i wish you hadn't gone to all the trouble, i was just looking for confirmation. :o)

but again, thank you.
daron

Luis Esquivel said:
Daron,

the vl-string-subst substitute only the first one the function finds.

something like this:

(defun string-to-list (str pat / i j n lst)
(cond
((/= (type str) (type pat) 'STR))
((= str pat) '(""))
(T
(setq i 0
n (strlen pat))
(while (setq j (vl-string-search pat str i))
(setq lst (cons (substr str (1+ i) (- j i)) lst)
i (+ j n)))
(mapcar '(lambda (x) (vl-string-trim " " x))
(reverse (cons (substr str (1+ i)) lst))))))

(apply 'strcat
(mapcar '(lambda (str) (strcat str "\\\\"))
(vl-remove-if-not
'(lambda (str) (/= str ""))
(string-to-list "g:\\test1\\test2\\" "\\"))))

"g:\\\\test1\\\\test2\\\\"

hth.

Daron Denton said:
could someone try replacing "\\" with "\\\\" using vl-string-translate or vl-string-subst?

(vl-string-subst "\\" "\\\\" "g:\\test1\\test2\\")
returns = "g:\\test1\\test2\\"

(vl-string-translate "\\" "\\\\" "g:\\test1\\test2\\")
returns = "g:\\test1\\test2\\"

(dos_strreplace "g:\\test1\\test2\\" "\\" "\\\\")
returns = "g:\\\\test1\\\\test2\\\\" <<<---this is what i'm looking for

thanks,
daron
 
Daron,
Confirmed(but you were doing it backwards) and it will only find the first instance from left. It couldn't find an instance of "\\\\"

(vl-string-subst "\\\\" "\\" "g:\\test1\\test2\\")
 
; Multiple substitution of a pattern string - 05/21/03
; Original by John Uhden, added SttPos argument
;
; Arguments:
; NewStr: the string to be substituted for pattern
; PatStr: a string containing the pattern to be replaced ; InpStr: the string to be searched for pattern ; SttPos: 0, nil or an integer identifying the starting position of the search ; ; Return Values: the value of string after any substitutions have been made ; ; Example:
; (ALE_StringSubstAll "new" "old" "old test for old method" nil) ; ; Notes:
; the search is case-sensitive
; SttPos = 0 or nil > substitution start from first character ; NewStr = "" remove all instances of PatStr from SttPos ; ; Suggestions:
; use (vl-string-translate "\t" " " "Hi\tthis\tis\ta\ttest!") ; for single (or set) character substitution (not to remove) ; (N.B. the inversion of arguments) ; (defun ALE_StringSubstAll (NewStr PatStr InpStr SttPos / NewLen) (setq NewLen (strlen NewStr)) (while (setq SttPos (vl-string-search PatStr InpStr SttPos)) (setq InpStr (vl-string-subst NewStr PatStr InpStr SttPos) SttPos (+ SttPos NewLen) ) ) InpStr )

Comando: (ALE_StringSubstAll "\\\\" "\\" "g:\\test1\\test2\\" nil)
"g:\\\\test1\\\\test2\\\\"
 
This might not be the most efficient, but it's
relatively bomb proof ( or that's the illusion
I'm enjoying ) - it won't be tripped up into
endless loops by identical or similar old and
new strings ( improvements anyone might have to
offer are welcomed ).

Of course, you'd never want to send it a old, new
or original string of "-=+#|\001*\377*\001|#+=-",
though I consider the odds of that coming up
*PRETTY* low.

Code:
(defun Replace ( OldStr NewStr Str / &Replace TempStr ) ;  local defun (defun &Replace ( OldStr NewStr Str / i ) (while (setq i (vl-string-search OldStr Str)) (setq Str (vl-string-subst NewStr OldStr Str i) ) ) Str ) ;  "main" (cond (  (or (eq OldStr NewStr) (eq "" OldStr)) Str) (  (eq NewStr "") (&Replace OldStr NewStr Str)) (  (vl-string-search OldStr NewStr) (&Replace (setq TempStr "-=+#|\001*\377*\001|#+=-") NewStr (&Replace OldStr TempStr Str) ) ) (  (&Replace OldStr NewStr Str)  ) ) )

(Replace "\\" "\\\\" "g:\\test1\\test2\\")

Returns "g:\\\\test1\\\\test2\\\\"

Cheers,

Michael.

~ ~ ~

"Daron Denton" <ask, and i'll give it to you> wrote:

could someone try replacing "\\" with "\\\\" using vl-string-translate or vl-string-subst?

(vl-string-subst "\\" "\\\\" "g:\\test1\\test2\\")
returns = "g:\\test1\\test2\\"

(vl-string-translate "\\" "\\\\" "g:\\test1\\test2\\")
returns = "g:\\test1\\test2\\"

(dos_strreplace "g:\\test1\\test2\\" "\\" "\\\\")
returns = "g:\\\\test1\\\\test2\\\\" <<<---this is what i'm looking for

thanks,
daron
 
Hi Michael,

O.S. = XP Pro 2002 - Sp.1 - Ita
AutoCAD = 2004 Ita
it won't be tripped up into endless loops by identical
or similar old and new strings

Comando: (ALE_StringSubstAll "\\" "\\" "g:\\test1\\test2\\" nil)
"g:\\test1\\test2\\"

My reports are:

(timerU '(Replace "\\" "\\\\" "g:\\test1\\test2\\") 100000)
100000 it.: 5.08 secs.

(timerU '(ALE_StringSubstAll "\\\\" "\\" "g:\\test1\\test2\\" nil) 100000)
100000 it.: 3.96 secs.

Cheers.
 
Bench this:

(timerU '(Replace "" "\\" "g:\\test1\\test2\\") 100000)

(timerU '(ALE_StringSubstAll "\\" "" "g:\\test1\\test2\\" nil) 100000)

;)

- - -

"Marc'Antonio Alessi" <maalessi at tin dot it> wrote:
Hi Michael,

O.S. = XP Pro 2002 - Sp.1 - Ita
AutoCAD = 2004 Ita
it won't be tripped up into endless loops by identical
or similar old and new strings

Comando: (ALE_StringSubstAll "\\" "\\" "g:\\test1\\test2\\" nil)
"g:\\test1\\test2\\"

My reports are:

(timerU '(Replace "\\" "\\\\" "g:\\test1\\test2\\") 100000)
100000 it.: 5.08 secs.

(timerU '(ALE_StringSubstAll "\\\\" "\\" "g:\\test1\\test2\\" nil) 100000)
100000 it.: 3.96 secs.

Cheers.
 
Ok, thanks:

(defun ALE_StringSubstAll (NewStr PatStr InpStr SttPos / NewLen) (if (= "" PatStr) InpStr (progn (setq NewLen (strlen NewStr)) (while (setq SttPos (vl-string-search PatStr InpStr SttPos)) (setq InpStr (vl-string-subst NewStr PatStr InpStr SttPos) SttPos (+ SttPos NewLen) ) ) InpStr ) )
)

(timerU '(Replace "" "\\" "g:\\test1\\test2\\") 100000)
100000 it.: 3.59 secs.

(timerU '(ALE_StringSubstAll "\\" "" "g:\\test1\\test2\\" nil) 100000)
100000 it.: 3.52 secs.
 

Latest threads

Back
Top