manipulation string

manipulation string

alexandre_benekowski
Advocate Advocate
1,067 Views
7 Replies
Message 1 of 8

manipulation string

alexandre_benekowski
Advocate
Advocate

Hi people,

 

I have a string that i´d like to count in a different way. I know (and use) "strlen" but it is not what i want. So, i´d like to count numbers of characters before "+" and after "+":

ex:

1+25

answer

1 and 2

 

100+25.00

answer

3 and 5

 

Someone could help me?

 

0 Likes
Accepted solutions (1)
1,068 Views
7 Replies
Replies (7)
Message 2 of 8

ronjonp
Mentor
Mentor

Quick example:

(defun _foo (string del / i)
  (if (setq i (vl-string-search del string))
    (list (setq i (strlen (substr string 1 i))) (- (strlen string) (1+ i)))
  )
)
(_foo "100+25.00" "+")
;; (3 5)

 

0 Likes
Message 3 of 8

doaiena
Collaborator
Collaborator

I've made a small edit to @Lee_Mac's String to List function. It will work with multiple entries.

;Lee Mac's LM:str->lst routine /with a small edit/
;http://lee-mac.com/stringtolist.html

(defun LM:str->lst ( str del / len lst pos ) (setq len (1+ (strlen del))) (while (setq pos (vl-string-search del str)) (setq lst (cons (strlen (substr str 1 pos)) lst) str (substr str (+ pos len)) ) ) (reverse (cons (strlen str) lst)) ) ;;;output: ;(LM:STR->LST "100+25.00+15+22.5+109" "+") ;(3 5 2 4 3)

 

0 Likes
Message 4 of 8

ronjonp
Mentor
Mentor

@doaiena wrote:

I've made a small edit to @Lee_Mac's String to List function. It will work with multiple entries.

(defun LM:str->lst ( str del / len lst pos )
    (setq len (1+ (strlen del)))
    (while (setq pos (vl-string-search del str))
        (setq lst (cons (strlen (substr str 1 pos)) lst)
              str (substr str (+ pos len))
        )
    )
    (reverse (cons (strlen str) lst))
)

;;;output:
;(LM:STR->LST "100+25.00+15+22.5+109" "+")
;(3 5 2 4 3)

 


FWIW .. no need to change Lee's code. To get the result you have shown, call it like this:

(mapcar 'strlen (lm:str->lst "100+25.00+15+22.5+109" "+"))
Message 5 of 8

doaiena
Collaborator
Collaborator

That works too. It just makes the statement a bit longer. Depends on @alexandre_benekowski's needs.

0 Likes
Message 6 of 8

ronjonp
Mentor
Mentor

Sure .. I'd think the outcome would be something more like this from the description.

(setq l (mapcar 'strlen (lm:str->lst "100+25.00+15+22.5+109" "+")))
(mapcar '(lambda (r j) (list r j)) l (cdr l))
;; '((3 5) (5 2) (2 4) (4 3))

 

0 Likes
Message 7 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

@alexandre_benekowski wrote:

... i´d like to count numbers of characters before "+" and after "+":

ex:

1+25

answer

1 and 2

 

100+25.00

answer

3 and 5

 

….

(setq str "1234+56")

Then:

(strcat

  (itoa (setq pre (vl-string-position (ascii "+") str)))

  " and "

  (itoa (- (strlen str) pre 1))

)

returns:
"4 and 2"

Kent Cooper, AIA
0 Likes
Message 8 of 8

alexandre_benekowski
Advocate
Advocate

I Kent!!!

thank you so very much!!!

 

It work!!!!

0 Likes