Jump to content

get specific values from string


ajithkumar.t

Recommended Posts

Hi,

For ex:"112+PL3/18+A36".This is my input string i want "PL3/18" and "A36" both are individually. Help me out this....

Link to comment
Share on other sites

Does the string alway have the same format? xyx '+' PL12/34 '+' A12

 

Where there is a '+' to separate the portions of the text? My favourite way to separate strings like this is to use Lee Macs String to List LISP and here, use + as the separator which will return a list like this (112 PL3/18 A36) containing 3 list items

 

Use nth or car, cadr, caddr to extract the text strings.

 

This works really well if the text is all in the same format and you want to get the same part of the text string, a bit trickier if there is no constant separator or if there are a different number of string segments in it

  • Like 1
Link to comment
Share on other sites

This is my code, but could not get output properly

(defun get-values-from-string (input-string)
  (setq pos1 (vl-string-search "+" input-string))
  (setq pos2 (vl-string-search "+" input-string (+ pos1 1)))
  (setq pos3 (vl-string-search "/" input-string))

  (if (and pos1 pos2)
      (list
       (strcase (substr input-string 1 (- pos1 1)))
       (if pos3
          (strcase (substr input-string (+ pos1 1) pos2))
         (strcase (substr input-string (+ pos1 1) (if pos2 pos2 (length input-string))))
          )
       (if pos3
          (strcase (substr input-string (+ pos2 1) pos2))
          (strcase (substr input-string (+ pos2 1)))
           )
      )
     (if pos3
         (list
          (strcase (substr input-string 1 (- pos3 1)))
         (strcase (substr input-string (+ pos3 1)))
         )
          (list (strcase input-string) "Not found" "Not found")
         )
     )
)

 

Link to comment
Share on other sites

(setq pos1 (vl-string-search "+" input-string)))
(setq pos2 (vl-string-search "+" input-string (+ pos1 1)))
(setq pos3 (vl-string-search "/" input-string))
(setq pos1a (1+ pos1))
(setq pos2a (1+ pos2))
(setq pos3a (1+ pos3))
(setq cad1 (strcase (substr input-string (1+ pos1a) (1- (- pos2a pos1a)))));return "PL3/18"
(setq cad2 (strcase (substr input-string (1+ pos2a))));return "A36"

 

  • Like 1
Link to comment
Share on other sites

For ex:"112+PL3/18+A36". Doing some googling came across a couple of examples of why do this, one was get distance between 2 points and add subtract some fixed amounts to the distance. If this is the case can do a lisp that reflects this type of answer. Post a dwg showing why your doing this calc. Ie text as formula. 

 

For Autocad users you have CAL you can type formulas there to be used in a command like line, you use 'CAL note the apostrophe. An obvious one is (pt1 pt2)/ 2 3 4 etc.

 

Have a look at the help for CAL.

Edited by BIGAL
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...