String characters?

Discussion in 'AutoCAD' started by BillZ, Nov 24, 2004.

  1. BillZ

    BillZ Guest

    R2005:

    I want to replace an underbar character in a string with some other character. What character other than alpha or numerical could I use that won't give me trouble later if it's considered a "special character".

    TIA

    Bill
     
    BillZ, Nov 24, 2004
    #1
  2. BillZ

    Doug Broad Guest

    Bill,
    You'll need to be more specific about the context. Are you speaking
    about AutoCAD object names, filenames, layer names....?

    Regards,
    Doug
     
    Doug Broad, Nov 24, 2004
    #2
  3. BillZ

    Joe Burke Guest

    Bill,

    Are you thinking about something like this?

    (setq str "a_b")
    (setq lst (vl-string->list str))
    (foreach x lst
    (if (= x 95)
    (setq lst (subst 37 x lst))
    )
    )
    (vl-list->string lst)

    Should return: "a%b", though not tested.

    Joe Burke
     
    Joe Burke, Nov 24, 2004
    #3
  4. BillZ

    GTASteve Guest

    As mentioned, depends on what you are refering to....

    If a BLOCK Name: the following characters are NOT okay:
    <>/\":;*|,=`
    or in english... ;-)
    (less than, greater than, slash, backslash, double quote, colon, semi-colon, asterix, pipe, comma, equals, back-tick)

    So, possible options for you would be:
    ~'!@#$%^&()-+{}[]?.
    or again, in english...
    (tilde, single quote, exclamation mark, commercial at symbol, hash mark, dollar sign, percent, carat, ampersand, left bracket, right bracket, hyphen, plus sign, left curly brace, right curly brace, left square bracket, right square bracket, question mark, period)

    If the intention, is to provide a visual separation of characters, and hyphen is not okay, you might consider period.

    e.g.

    "48x36.Double.Hung.Window"

    I believe the rules for namiing Layers, Views, etc. are similar, but if in doubt, just test the characters in a dummy Layer, View, etc.

    Note: Regardless of what you choose, the *further* implications, of what you choose, may be interesting.... E.g. if you later want to export your info to Excel, in a CDF file, a (') single quote, may cause "weirdities" etc.

    Anyway, let us know what it is you plan to name, and I'm sure we will be able to help more...

    Cheers,
    Steve
     
    GTASteve, Nov 24, 2004
    #4
  5. BillZ

    BillZ Guest

    Thanks Joe,

    That idea may come in handy some time.

    Bill
     
    BillZ, Nov 24, 2004
    #5
  6. PMJI, but your code below has left me scratching my head
    (which is not good).

    What is the point to the foreach..if.. business?

    IOW and discounting for a moment the more direct
    approach using VL-STRING-SUBST, is there something
    wrong with just a single subst here?

    (setq str "a_b")
    (vl-list->string
    (subst 37 95
    (vl-string->list str)
    )
    )

    Or for that matter:

    (vl-string-subst "%" "_" "a_b")
     
    Tony Tanzillo, Nov 24, 2004
    #6
  7. BillZ

    Joe Burke Guest

    Tony,

    I sort of live in fear of you looking at code I post. Because I'm well aware it's
    often amateurish. Not efficient goes hand-in-hand. But that won't stop me from
    posting, as long as you can tolerate my ineptness.

    Thanks for your comments. They will be taken to heart, at least on my end. I hope
    others will benefit as well.

    Joe Burke
     
    Joe Burke, Nov 24, 2004
    #7
  8. Joe - I don't regard your code as amateurish, and
    efficiency isn't a prerequisite.

    I just found myself scratching my head when I
    looked at that, because it seemed redundant.
     
    Tony Tanzillo, Nov 24, 2004
    #8
  9. BillZ

    Joe Burke Guest

    Tony,

    I took what I posted from something I use which replaces any numeric character in a
    string, with some other character. In the example below, numbers are replaced with
    "X". Applying this method to what Bill asked for, naturally produced head scratching.
    :)

    I wonder if there's a better way to do this? Suggestions welcome.

    (setq str "1,233 SF")
    (setq lst (vl-string->list str))
    (foreach x lst
    (if (< 47 x 58)
    (setq lst (subst 88 x lst))
    )
    )
    (vl-list->string lst) returns "X,XXX SF"

    Thanks
    Joe Burke
     
    Joe Burke, Nov 25, 2004
    #9
  10. BillZ

    Doug Broad Guest

    Joe,

    One way (not sure its better).

    (mapcar '(lambda (x) (if (< 47 x 58) 88 x)) lst)

    Happy Thanksgiving
     
    Doug Broad, Nov 25, 2004
    #10
  11. In terms of best practices for the sake of performance,
    see my previous posts regarding the use of destructive
    list editing functions (which subst is one of).

    Each call to subst allocates and returns a *copy* of the
    input list, which makes it somewhat inefficient in this
    context.

    mapcar might be a better choice.

    (vl-list->string
    (mapcar
    '(lambda (char)
    (if (> 58 char 47) 88 char)
    )
    (vl-string->list <input string>)
    )
    )
     
    Tony Tanzillo, Nov 25, 2004
    #11
  12. BillZ

    Joe Burke Guest

    Thanks guys.

    Joe Burke
     
    Joe Burke, Nov 26, 2004
    #12
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.