[Newbies] String construction and replacement
claus_kick at web.de
Mon Feb 2 21:25:29 UTC 2009
Randal L. Schwartz wrote:
>>>>>>"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.
Honestly, I never think of Streams in this context. One day, I might
actually remember to use them.
> For example, look at the implementation of String>>expandMacrosWithArguments:
> to see how to use a ReadStream on the source and WriteStream to hold the
Ah, ok, I will really note this down this time.
> Also, if the logic gets to be nested blocks, I try to refactor that
> rather quickly.
How would you refactor something like that?
More information about the Beginners