change the text value from string

change the text value from string

avinash00002002
Collaborator Collaborator
798 Views
5 Replies
Message 1 of 6

change the text value from string

avinash00002002
Collaborator
Collaborator

Hi!

I have some strings which has space in left , middle , and right side and there is not equal space every time.

like...

" 2311 51"
" 51 2552"
" 51 2612"

" 153 51"

 

I want to change its value by +50. any idea how to do.

 

Thanks,

 

Avinash

0 Likes
799 Views
5 Replies
Replies (5)
Message 2 of 6

hak_vz
Advisor
Advisor

Here you have some functions to use.

Check how this function work and combine to your code. Since you are learning to code in lisp

this is good exercise for you. 

(defun rem1 (str) (vl-string-trim " " str))
(defun rem2 (str / str)
 (while (vl-string-search "  " str)
	(setq str (vl-string-subst " " "  " str))
 )
  (while (vl-string-search " " str)
	(setq str (vl-string-subst "," " " str))
 )
)

(defun str_to_lst (str /  lst pos )
    (while (setq pos (vl-string-search "," str))
        (setq lst (cons (substr str 1 pos) lst)
              str (substr str (+ pos (1+ (strlen ","))))
        )
    )
    (reverse (cons str lst))
)


(defun string_to_list ( str del / pos )
	(if (setq pos (vl-string-search del str))
		(cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
		(list str)
	)
) 

Also

(vl-string-left-trim " 2311 51" " ")

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 3 of 6

CADaSchtroumpf
Advisor
Advisor

Hi,

(defun vl-position-multi (el l / n l_id l_n)
	(setq
		n 0
		l_id (mapcar '(lambda (x) (equal x el)) l)
	)
	(repeat (length l_id)
		(if (car l_id) (setq l_n (cons n l_n)))
		(setq n (1+ n) l_id (cdr l_id))
	)
	(reverse l_n)
)
(defun foo (str / l_cv l_pos nw_val)
	(setq
		l_cv (vl-string->list str)
		l_pos (vl-position-multi 32 l_cv)
		nw_val (itoa (+ 50 (atoi (vl-list->string (vl-remove 32 l_cv)))))
	)
	(foreach n l_pos
		(setq nw_val (vl-string-subst (strcat " " (substr nw_val (1+ n) 1)) (substr nw_val (1+ n) 1) nw_val n))
	)
	nw_val
)

Command: (foo " 2311 51")
" 2312 01"

Command: (foo " 51 2552")
" 51 2602"

Command: (foo " 51 2612")
" 51 2662"

Command: (foo " 153 51")
" 154 01"

0 Likes
Message 4 of 6

Kent1Cooper
Consultant
Consultant

The usage examples in Message 3 raise the question:  What is "it" that you want to increase by 50?  The entire string as if the space were a decimal point?  If so, do you really want to increase it by 0.50, or should it be as if the space were not there, rather than a decimal point?  Or do you want to increase just the part after the space, or maybe the part before the space?  In other words, should " 2311 51" be increased to " 2312 01" as in Message 3's result, or to " 2311 101", or maybe " 2361 51", or something else?

Kent Cooper, AIA
0 Likes
Message 5 of 6

3wood
Advisor
Advisor

You can also try ALTEXT.

It works in all scenario except there is only one number after the space.

ALTEXT7.gif

 

0 Likes
Message 6 of 6

Sea-Haven
Mentor
Mentor

My $0.05 you can change the delimeter by thinking this way.

 

; thanks to Lee-mac for this defun 
; www.lee-mac.com
; 44 is comma 32 is space 59 is semicolon
(defun _csv->lst ( strtxt / pos )
	(if (setq pos (vl-string-position 32 strtxt))
		(cons (substr strtxt 1 pos) (_csv->lst (substr strtxt (+ pos 2))))
		(list strtxt)
    )
)

 

0 Likes