Author Topic: string manipulation  (Read 5501 times)

0 Members and 1 Guest are viewing this topic.

matrix2005in

  • Guest
string manipulation
« 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

T.Willey

  • Needs a day job
  • Posts: 5251
Re: string manipulation
« Reply #1 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
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: string manipulation
« Reply #2 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?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

matrix2005in

  • Guest
Re: string manipulation
« Reply #3 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

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: string manipulation
« Reply #4 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")

« Last Edit: June 27, 2006, 01:03:39 AM by ElpanovEvgeniy »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: string manipulation
« Reply #5 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"

Sdoman

  • Guest
Re: string manipulation
« Reply #6 on: June 27, 2006, 08:15:18 AM »

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

Very elegant solutions, Elpanov!

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: string manipulation
« Reply #7 on: June 27, 2006, 08:42:53 AM »
Very elegant solutions, Elpanov!

Thank!  :-)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: string manipulation
« Reply #8 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)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

LE

  • Guest
Re: string manipulation
« Reply #9 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")

matrix2005in

  • Guest
Re: string manipulation
« Reply #10 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??

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: string manipulation
« Reply #11 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 ... ?

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: string manipulation
« Reply #12 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"

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: string manipulation
« Reply #13 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.......

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: string manipulation
« Reply #14 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.





kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: string manipulation
« Reply #15 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
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

matrix2005in

  • Guest
Re: string manipulation
« Reply #16 on: July 02, 2006, 12:22:22 AM »
hi

this is what i want excellent code...thanks verymuch

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: string manipulation
« Reply #17 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!

Andrea

  • Water Moccasin
  • Posts: 2372
Re: string manipulation
« Reply #18 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")
« Last Edit: July 02, 2006, 08:11:06 PM by Andrea »
Keep smile...

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: string manipulation
« Reply #19 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)
                     )
    )
)
;;---------------------------------------------------------------------------
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: string manipulation
« Reply #20 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