vl-string-translate question

Discussion in 'AutoCAD' started by Daron Denton, Jan 19, 2004.

  1. Daron Denton

    Daron Denton Guest

    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 Denton, Jan 19, 2004
    #1
  2. 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.

    --
    Get your free DRAFTTEAM version 1.4 cop(ies)!,
    How? download it from here: http://www.draftteam.com/draftteam/draftteam.zip
    Or sent your request to:

    [A comprehensive set of drafting routines to enhance and supplement the use
    of AutoCAD.]

    Offer ends on Friday, January 23 of 2004
     
    Luis Esquivel, Jan 19, 2004
    #2
  3. Daron Denton

    Daron Denton Guest

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

    but again, thank you.
    daron


     
    Daron Denton, Jan 19, 2004
    #3
  4. Daron Denton

    Doug Broad Guest

    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\\")
     
    Doug Broad, Jan 19, 2004
    #4
  5. ; 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\\\\"


    --
    ________________________________________________

    Marc'Antonio Alessi (TV) Italy
    (strcat "NOT a " (substr (ver) 8 4) " guru.")

    O.S. = XP Pro 2002 - Sp.1 - Ita
    AutoCAD = 2004 Ita
    ________________________________________________
     
    Marc'Antonio Alessi, Jan 20, 2004
    #5
  6. 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 in message

    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
     
    michael puckett, Jan 20, 2004
    #6
  7. Hi Michael,
    ________________________________________________

    O.S. = XP Pro 2002 - Sp.1 - Ita
    AutoCAD = 2004 Ita
    ________________________________________________
    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.
     
    Marc'Antonio Alessi, Jan 20, 2004
    #7
  8. 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 in message
    Hi Michael,
    ________________________________________________

    O.S. = XP Pro 2002 - Sp.1 - Ita
    AutoCAD = 2004 Ita
    ________________________________________________
    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.
     
    michael puckett, Jan 20, 2004
    #8
  9. 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.
     
    Marc'Antonio Alessi, Jan 21, 2004
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.