[Newbies] String construction and replacement

David Mitchell david.mitchell at gmail.com
Sun Feb 1 21:12:12 UTC 2009


Great question.

To deal with the input characters, it is useful to have them in an
array. A traditional Smalltalk array with characters would look like
this #($a $b $c) but it isn't obvious what to do with the carriage
return. For this, the Squeak brace array is handy and also works in
Pharo.

characters := {$H.$I.Character cr. $T.$H.$E.$R.$E}.

It isn't terribly portable to other Smalltalks, but it sure is easy to
type. Now we've got an array of characters, how to create the new
String? Another Squeak-ism is the class method #streamContents:. It
takes a one argument block. The argument is a writeable stream that
will return its contents at the end. I have to admit I was thrown by
it the first time I saw it.

string := String streamContents: [:writeStream | characters do: [:c |
writeStream nextPut: c]].
Like the brace array, it isn't very portable to other Smalltalks, but
it is pretty handy.

That leaves us with replacing. I opened the method finder and typed
replace into the search box.

That gives us the following complete solution:

| characters string |
characters := {$H.$I.Character cr. $T.$H.$E.$R.$E}.
string := String streamContents: [:writeStream | characters do: [:c |
writeStream nextPut: c]].
string copyReplaceAll: 'HI' with: 'HELLO'

I also posted this to my blog (with a picture of the Method Finder)

http://www.withaguide.com/2009/02/characters-strings-and-things.html

On Sun, Feb 1, 2009 at 1:46 PM, Sebastian Nozzi <sebnozzi at googlemail.com> wrote:
>
> Hello List,
>
> I've been struggling a bit with some basics about Strings for which I
> couldn't find an answer (yet).
>
> 1) How to construct a String from Characters?
>
> For example, I want to construct a String from the Chatacter literals:
>
> $H $I
> Character cr
> $T $H $E $R $E
>
> 2) How to replace a sequence of Characters in a String for others?
>
> For exaple, I want to replace every 'HI' (in this case only one) for
> 'HELLO' in the String above (not necesarily destructively, getting a
> new String is also ok). Is there a quick way to do that?
>
> Thanks in advance!
>
> Sebastian
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


More information about the Beginners mailing list