[Newbies] String construction and replacement

Randal L. Schwartz merlyn at stonehenge.com
Mon Feb 2 21:12:12 UTC 2009


>>>>> "Claus" == Claus Kick <claus_kick at web.de> writes:

Claus> pattern := 'HI'.
Claus> replacement := 'HELLO'.
Claus> resultString := ''.
Claus> i := 1.
Claus> answer do:[:element | (element = pattern ) ifTrue:[i = 1 ifTrue:[resultString
Claus> := resultString, replacement . i := i + i] ifFalse:[resultString :=
Claus> resultString, ' ', replacement ]] ifFalse:[resultString := resultString, ' ',
Claus> element]].
Claus> ^resultString.

That sort of "build a string by repeated concatenation" scares me.  Might
be ok for very short things, but for longer things, learn to use streams.

For example, look at the implementation of String>>expandMacrosWithArguments:
to see how to use a ReadStream on the source and WriteStream to hold the
destination.

Also, if the logic gets to be nested blocks, I try to refactor that
rather quickly.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


More information about the Beginners mailing list