[Newbies] String construction and replacement

Sebastian Nozzi sebnozzi at googlemail.com
Mon Feb 2 10:57:51 UTC 2009


To all whom I wrote that I found it strange that there was no class
method to construct a String from an array... I was wrong!

I debugged Randal's suggestion of using "as: String" and the result
that "newFrom: aCollection" is called. So, the solution could also be:

string := String newFrom: {$H.$I.Character cr. $T.$H.$E.$R.$E}.

Of course, to make it completely portable you could also use two ways
that have been suggested: add every character into an
OrderedCollection and use that, or use a standard array-literal and
manually #put: the carriage-return afterwards.

Thank you again for your answers,

Sebastian


> 2009/2/1 Randal L. Schwartz <merlyn at stonehenge.com>:
>>>>>>> "David" == David Mitchell <david.mitchell at gmail.com> writes:
>>
>> David> | characters string |
>> David> characters := {$H.$I.Character cr. $T.$H.$E.$R.$E}.
>> David> string := String streamContents: [:writeStream | characters do: [:c |
>> David> writeStream nextPut: c]].
>>
>> chars := {$H.$I.Character cr. $T.$H.$E.$R.$E}.
>> string := chars as: String.
>>
>> Far simpler.  Look at #as:... it's a pretty good "force this into that"
>> call.
>>
>> --
>> 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