(newbie) String / Array / ArrayedCollection

David T. Lewis lewis at mail.msen.com
Thu Dec 29 21:24:49 UTC 2005


Quite right. For example:

(String streamContents: [:stream |
  'A series of words to be shuffled' substrings shuffled
    do: [:e | stream nextPutAll: e; nextPut: Character space]]) allButLast

or:

String streamContents: [:stream |
  'A series of words to be shuffled' substrings shuffled
    inject: ''
    into: [:sep :e | stream nextPutAll: sep; nextPutAll: e.
          ' ']]


On Sun, Jan 01, 2006 at 06:02:18PM +0200, Daniel Vainsencher wrote:
> There a method that streamlines efficient string creation called 
> stringContents, or streamContents, or contentString, or something like 
> that (don't remember).
> 
> theString _ String <methodName>: [:ws |
> 'A series of words to be shuffled' substrings shuffled
> 	do: [:e | ws nextPutAll: e; nextPut: Character space]]
> 
> Daniel
> 
> David T. Lewis wrote:
> > 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