string search...and count..?

Discussion in 'AutoCAD' started by C Witt, Sep 10, 2004.

  1. C Witt

    C Witt Guest

    I need a way of searching a string to "count" how many times string "a"
    apears in string "b".<br>
    <br>
    ie:<br>
    string b: "The house is a tall one also ." (the string to be
    seached)<br>
    string a: " " (double space)<br>
    <br>
    This may seem odd to some of you (or all)..<br>
    But it's part of a much larger lisp.<br>
    <br>
    TIA.
     
    C Witt, Sep 10, 2004
    #1
  2. C Witt

    T.Willey Guest

    Try this.

    Command: (searchstring "hellolo" "lo")
    2

    Tim

    (defun SearchString (Str1 Str2 / cnt2)

    (setq slng1 (strlen Str2)
    cnt2 0
    )
    (while (>= (strlen Str1) slng1)
    (if (= Str2 (substr Str1 1 slng1))
    (setq cnt2 (1+ cnt2))
    )
    (setq Str1 (substr Str1 2 (strlen Str1)))
    )
    cnt2
    )
     
    T.Willey, Sep 10, 2004
    #2
  3. C Witt

    Jeff Mishler Guest

    Here's some sample code to get you going. If the var "found" is not nil then
    the pattern was found. The length of "found" will tell you haw many times it
    was found. Each element of found gives the index position in the string
    where it was found.

    (setq str "This is a sample string")
    (setq srchstr " ");double space
    (setq test -1)
    (while (setq test (vl-string-search srchstr str (1+ test)))
    (setq found (cons test found))
    )
    (setq found (reverse found))

    This returns: (4 10 18)

    HTH,
     
    Jeff Mishler, Sep 10, 2004
    #3
  4. C Witt

    T.Willey Guest

    I like your's better Jeff. Returns more options incase you need that info later.

    Tim
     
    T.Willey, Sep 10, 2004
    #4
  5. C Witt

    C Witt Guest

    thank you very much.


     
    C Witt, Sep 10, 2004
    #5
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.