(newbie) String / Array / ArrayedCollection

David T. Lewis lewis at mail.msen.com
Thu Dec 29 18:29:25 UTC 2005


On Thu, Dec 29, 2005 at 11:32:53AM -0600, Tim Johnson wrote:
> 
> Quick question.  What is the quick way to get an Array of strings back 
> into one String?  Do I have to use a ReadStream / WriteStream?
> 
> Example:
> 
> 'A series of words to be shuffled' substrings shuffled
>       ->    #('of' 'series' 'words' 'A')
> 

A WriteStream is usually a good way to do this, something like this:

| ws |
ws := WriteStream on: ''.
'A series of words to be shuffled' substrings shuffled
  do: [:e | ws nextPutAll: e; nextPut: Character space].
ws contents allButLast

But you can also iterate over a collection and accumulate the results
using #inject:into:, for example like this:

'A series of words to be shuffled' substrings shuffled
    inject: nil
    into: [:str :e | (str ifNil: [''] ifNotNil: [str, ' ']), e]

or this:

('A series of words to be shuffled' substrings shuffled
    inject: ''
    into: [:str :e | str, ' ', e]) allButFirst

Dave
 



More information about the Squeak-dev mailing list