String handling gives stranges results

Discussion in 'AutoCAD' started by Jimmy D, Feb 2, 2005.

  1. Jimmy D

    Jimmy D Guest

    Can anyone tell me what I'm doing wrong??

    If I use this string in my code:

    (setq Position T)
    (setq TestString "999/288/abc/777/abcdef")
    (while (/= Position nil)
    (setq Position (vl-string-search "/" TestString))
    (setq SubTestString (substr TestString 1 Position))
    (setq TestString (vl-string-left-trim (strcat SubTestString "/") TestString))
    (princ "\n-")
    (princ SubTestString)
    );end while

    the result is:
    -999
    -288
    -abc
    -777
    -abcdef"abcdef"

    but if I use this string in the same code:

    (setq TestString "299/288/abc/777/abcdef") -->999 is now 299

    the result is:
    -299
    -88 <-----??
    -abc
    -777
    -abcdef"abcdef"

    Where did my "2" from 288 go to?

    Thanks,
    Jim
     
    Jimmy D, Feb 2, 2005
    #1
  2. Jimmy D

    Jeff Mishler Guest

    Read the description for (vl-string-left-trim) more carefully.
    "Removes the specified characters from the beginning of a string"
    It's not saying "removes pattern"......so it removes from the left string
    until no match is made.
    In the case of "299/" and "299/288/abc/777/abcdef" the first mis-match is at
    the 8.......
    So this works:

    (while (setq Position (vl-string-search "/" TestString))
    (setq SubTestString (substr TestString 1 Position))
    (setq TestString (substr TestString (+ 2 position)))
    (princ "\n-")
    (princ SubTestString)
    );end while
    (if (< 0 (strlen TestString))
    (progn
    (princ "\n-")
    (princ TestString)
    )
    )
     
    Jeff Mishler, Feb 2, 2005
    #2
  3. Jimmy D

    BillZ Guest

    I don't know what you're doing wrong but while playing with these functions I came up with something that could be useful.

    (setq TestString "999/288/abc/777/abcdef")
    "999/288/abc/777/abcdef"

    (while (vl-string-search "/" teststring)(setq teststring
    (vl-string-subst " " "/" teststring)))
    "999 288 abc 777 abcdef"

    (mapcar 'vl-princ-to-string (read (strcat "(" teststring ")")))
    ("999" "288" "ABC" "777" "ABCDEF")

    Works with forward slahes too:

    (setq TestString "999\\288\\abc\\777\\abcdef")
    "999\\288\\abc\\777\\abcdef"

    (while (vl-string-search "\\" teststring)(setq teststring
    (vl-string-subst " " "\\" teststring)))
    "999 288 abc 777 abcdef"

    (mapcar 'vl-princ-to-string (read (strcat "(" teststring ")")))
    ("999" "288" "ABC" "777" "ABCDEF")



    Bill
     
    BillZ, Feb 2, 2005
    #3
  4. Jimmy D

    Jimmy D Guest

    Jeff, of course you're right. It's our disadvantage that English isn't our mother tongue and sometimes it is really hard to fully understand the explanation given in the help files (although I'm not trying to excuse myself :) ".
    Thanks for your help!

    Jimmy
     
    Jimmy D, Feb 3, 2005
    #4
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.