[Newbies] String construction and replacement

Ken G. Brown kbrown at mac.com
Mon Feb 2 15:03:32 UTC 2009


Is there something wrong with just doing:

string := 'Hello
There'.

?

Ken G. Brown


>Date: Mon, 2 Feb 2009 11:57:51 +0100
>From: Sebastian Nozzi <sebnozzi at googlemail.com>
>Subject: Re: [Newbies] String construction and replacement
>To: Beginners at lists.squeakfoundation.org
>Message-ID:
>	<1e8ee5cb0902020257s71b52e5m96772ea151ad5928 at mail.gmail.com>
>Content-Type: text/plain; charset=UTF-8
>
>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