split from string

split from string

avinash00002002
Collaborator Collaborator
474 Views
2 Replies
Message 1 of 3

split from string

avinash00002002
Collaborator
Collaborator

Hi!

 

I have a situation that I have a string and I want to split it like....

 

"L 75x75x5-LG... 1240" TO ("L"  "75" "75" "5" "LG" "1240")

AND 

"L 75x75x5" TO ("L" "75" "75" "5")

AND

"LG.1240" TO ("LG" "1240")

AND

"L 75x5" TO ("L" "75" "5")

 

is it possible?

 

Thanks,

 

Avinash

0 Likes
Accepted solutions (1)
475 Views
2 Replies
Replies (2)
Message 2 of 3

komondormrex
Mentor
Mentor
Accepted solution

hey there,

check the function

(defun string_to_list (string / _list)
	(setq _list (vl-string->list string)) 
	(foreach delimiter (vl-string->list "x.-")
		(setq _list (subst 32 delimiter _list))
	)
	(mapcar '(lambda (element) (vl-princ-to-string element))
			 (read (strcat "(" (vl-list->string _list) ")"))
	)
)

using

 

Command: (string_to_list "L 75x75x5-LG... 1240")
("L" "75" "75" "5" "LG" "1240")

Command: (string_to_list "L 75x75x5")
("L" "75" "75" "5")

Command: (string_to_list "LG.1240")
("LG" "1240")

Command: (string_to_list "L 75x5")
("L" "75" "5")

 

0 Likes
Message 3 of 3

ronjonp
Mentor
Mentor

@avinash00002002 @komondormrex 

Here's another way to write that function:

(defun _s2l (str)
  (mapcar 'vl-princ-to-string (read (strcat "(" (vl-string-translate ".x-" "   " str) ")")))
)
(_s2l "L 75x75x5-LG... 1240")
;; ("L" "75" "75" "5" "LG" "1240") 

 

0 Likes