TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: matrix2005in on June 26, 2006, 02:35:53 PM

Title: string manipulation
Post by: matrix2005in on June 26, 2006, 02:35:53 PM
how i can divide combined strings to equal number of strings?/

eg:- the combined string is - ABCDEFGHIJKL
i would like to divide into - ABC DEF GHI JKL

how i can do it??

thanks
mathew
Title: Re: string manipulation
Post by: T.Willey on June 26, 2006, 02:58:09 PM
Find out how long the stirng is, then find the number of new strings you want.  Then use substr.

Hint: strlen, loop with while, substr
Title: Re: string manipulation
Post by: MP on June 26, 2006, 03:16:39 PM
What do you want the final product to be (a string or a list) and what should the function do when passed strings that don't divide evenly?
Title: Re: string manipulation
Post by: matrix2005in on June 26, 2006, 10:47:06 PM
A string only..but that must be divided equally...the input will be even number and number of strings will be predefined..eg:
input string: GHJLIETYHDWR =12(this will be predefined)
Output must be = GHJL IETY HDWR

thanks

mathew
Title: Re: string manipulation
Post by: ElpanovEvgeniy on June 27, 2006, 12:47:17 AM
A string only..but that must be divided equally...the input will be even number and number of strings will be predefined..eg:
input string: GHJLIETYHDWR =12(this will be predefined)
Output must be = GHJL IETY HDWR

thanks

mathew


Hi Mathew
Code: [Select]

(defun str-trim-i (str i)
   (if (> (strlen str) 0)
      (cons (substr str 1 i) (str-trim-i (substr str (1+ i)) i))
   ) ;_  if
) ;_  defun

(str-trim-i  "GHJLIETYHDWR" 2)  ; ->> ("GH" "JL" "IE" "TY" "HD" "WR")
(str-trim-i  "GHJLIETYHDWR" 3)  ; ->> ("GHJ" "LIE" "TYH" "DWR")
(str-trim-i  "GHJLIETYHDWR" 4)  ; ->> ("GHJL" "IETY" "HDWR")
(str-trim-i  "GHJLIETYHDWR" 5)  ; ->> ("GHJLI" "ETYHD" "WR")
(str-trim-i  "GHJLIETYHDWR" 6)  ; ->> ("GHJLIE" "TYHDWR")

Title: Re: string manipulation
Post by: ElpanovEvgeniy on June 27, 2006, 01:11:29 AM
Variant with string...  :-)

Code: [Select]
(defun str-trim-i>str (str i)
 (if (> (strlen str) i)
  (strcat (substr str 1 i) " " (str-trim-i>str (substr str (1+ i)) i))
  str
 ) ;_  if
) ;_  defun
(str-trim-i>str "GHJLIETYHDWR" 2) ;->> "GH JL IE TY HD WR"
(str-trim-i>str "GHJLIETYHDWR" 3) ;->> "GHJ LIE TYH DWR"
(str-trim-i>str "GHJLIETYHDWR" 4) ;->> "GHJL IETY HDWR"
(str-trim-i>str "GHJLIETYHDWR" 5) ;->> "GHJLI ETYHD WR"
(str-trim-i>str "GHJLIETYHDWR" 6) ;->> "GHJLIE TYHDWR"
Title: Re: string manipulation
Post by: Sdoman on June 27, 2006, 08:15:18 AM

(defun str-trim-i (str i) ...
   

Very elegant solutions, Elpanov!
Title: Re: string manipulation
Post by: ElpanovEvgeniy on June 27, 2006, 08:42:53 AM
Very elegant solutions, Elpanov!

Thank!  :-)
Title: Re: string manipulation
Post by: CAB on June 27, 2006, 11:07:50 AM
Very nice Evgeniy!
And for a non-recursive version:
Code: [Select]
(defun string-break (str brk@ / idx result tmp)
  (setq idx (- 1 brk@))
  (while (/= (setq tmp (substr str (setq idx (+ idx brk@)) brk@)) "")
    (if result
      (setq result (strcat result " " tmp))
      (setq result tmp)
    )
  )
  result
)


Code: [Select]
(defun string-break2 (str brk@ / idx result tmp)
  (setq idx (- 1 brk@))
  (while (/= (setq tmp (substr str (setq idx (+ idx brk@)) brk@)) "")
    (if result
      (setq result (cons tmp result))
      (setq result (list tmp))
    )
  )
  (reverse result)
)
Title: Re: string manipulation
Post by: LE on June 27, 2006, 12:20:50 PM
Code: [Select]
(defun break_string  (txt cnt / string strings)
  (setq string (substr txt 1 cnt))
  (setq strings (cons string strings))
  (while (/= (setq txt (vl-string-left-trim string txt)) "")
    (if (/= (setq string (substr txt 1 cnt)) "")
      (setq strings (cons string strings))))
  (reverse strings))

Quote
(break_string "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 3)
("ABC" "DEF" "GHI" "JKL" "MNO" "PQR" "STU" "VWX" "YZ")
Title: Re: string manipulation
Post by: matrix2005in on July 01, 2006, 12:51:54 PM
hi thanks that's gr8

another question..regarding looping....code given below is to strip off characters from a set of word..

(defun test ()
  (setq afg "GHJ5623JHG56")
  (setq drv (dos_strremove (strcat afg) "G"))
  (setq drv (dos_strremove (strcat DRV) "H"))
  (setq drv (dos_strremove (strcat drv) "J"))
)

actually variable afg wont be predefined like i mentioned here...afg will be include any letters that i dont know so i need to add all alphabet here...instead of repeating same code is there any other way to do this..looping or something??
Title: Re: string manipulation
Post by: Kerry on July 01, 2006, 06:51:18 PM
It must be my Sunday morning brain fog ... but I cant determine your requirements.

Do you want the string stripped of ALL alpha characters, upper and lower case ???
... only returning numeric digits ?

.... OR ... ?

Title: Re: string manipulation
Post by: Kerry on July 01, 2006, 06:55:25 PM
if ... only returning numeric digits ?
something like this may suit you ...

Code: [Select]
;;---------------------------------------------------------------------------
(DEFUN numsOnly (str /)
    (VL-LIST->STRING (VL-REMOVE-IF-NOT 'numchar-p (VL-STRING->LIST str)))
)
;;---------------------------------------------------------------------------
(DEFUN numchar-p (n) (AND (> n 47) (< n 58)))
;;---------------------------------------------------------------------------


Quote
(setq afg "GHJ5623JHG56")

;; ...

(setq stripper (numsOnly afg))


;;=> "562356"

Title: Re: string manipulation
Post by: Jeff_M on July 01, 2006, 07:12:44 PM
Kerry, if it's any consolation, that's what my Saturday afternoon brain thought was wanted as well.   :| I was waiting for the task to be clarified.......
Title: Re: string manipulation
Post by: Kerry on July 01, 2006, 08:06:43 PM
Hi Jeff,
just checked my news/mail .. looks like Jon on the newsgroup read the same question another way ...
.. and gave a replacement functionality for dos_strremove.

so, yes, just goes to show.

I have always maintained that the question is more important than the answer. .. but perhaps thats my personal problem.





Title: Re: string manipulation
Post by: CAB on July 01, 2006, 08:22:35 PM
Quote
It is better to find approximate solution to an exact problem than to find an exact solution to approximate problem.
Author Unknown
Title: Re: string manipulation
Post by: matrix2005in on July 02, 2006, 12:22:22 AM
hi

this is what i want excellent code...thanks verymuch
Title: Re: string manipulation
Post by: Jeff_M on July 02, 2006, 01:26:00 AM
I want excellent code, too, but alas, all I have is me to write it .....  :evil:

mathew, a well placed period "." or comma "," would change the meaning of that sentence quite a bit.... :-)

Kerry, I knew we were right and Jon was missing the mark.  :kewl:

CAB, well quoted!
Title: Re: string manipulation
Post by: Andrea on July 02, 2006, 08:01:23 PM
this is mine...
Code: [Select]
(setq t1 "ABCDEFGHIJK")
(setq list2 nil)
(while (/= t1 "")
  (setq list1 (substr t1 1 3))
  (setq list2 (append list2 (list list1)))
  (setq t1 (substr t1 4))
)
(foreach n list2
  (if (not text1)(setq text1 " "))
  (setq text1 (strcat text1 " " n))
)
(setq text2 (substr text1 3))

text2 = "ABC DEF GHI JK"

list2 = ("ABC" "DEF" "GHI" "JK")
Title: Re: string manipulation
Post by: Kerry on July 03, 2006, 06:17:12 AM
hi

...thanks verymuch

Have a play with this too .. though not as easy to understand, perhaps ..

Code: [Select]
;;---------------------------------------------------------------------------
(DEFUN numsOnly (str /)
    (VL-LIST->STRING (VL-REMOVE-IF-NOT '(LAMBDA (n) (AND (> n 47) (< n 58)))
                                       (VL-STRING->LIST str)
                     )
    )
)
;;---------------------------------------------------------------------------
Title: Re: string manipulation
Post by: ElpanovEvgeniy on July 03, 2006, 07:46:51 AM
Code: [Select]
(defun test (str)
  ; Not used VL-...
  (if (= str "")
    ""
    (if (< 47 (ascii str) 58)
      (strcat
(substr str 1 1)
(test (substr str 2))
      ) ;_  strcat
      (test (substr str 2))
    ) ;_  if
  ) ;_  if
) ;_  defun