Take the 2-minute tour ×

simple question i got

|list string|
list:= #('ab' 'efghij' 'lmnopqrst'). "Ordered collection"
list do:[:each| "string with:each"  i know this is not right how do i add items ].

I tried streams too it returned me this "an ordered collection('ab' 'efghij' 'lmnopqrst')"

All i need is a single Text that has

'abc efghij lmnopqrst '
share|improve this question

2 Answers 2

up vote 4 down vote accepted

In Pharo you can do

Character space join: list

If join: is not available and it should perform well then you can use a stream variant

String streamContents: [:stream| 
        do [:each| stream nextPutAll: each ]
        separatedBy: [ stream nextPut: Character space ]
share|improve this answer

Object class has defined a #asString message that reads:

"Answer a string that represents the receiver."

So, you can do:

| aList aStringsList |
aList := #('ab' 'efghij' 'lmnopqrst'). "Array"
aStringsList := aList collect: [ :each | each asString ]

And aStringsList will be an Array of the Strings returned by the invocation of #asString in each of aList's members.

If you want to concatenate all of them in a single String, you can use the #inject:into: method of the collections instead of #collect::

aList inject: '' into: [ :text :each | text , each asString , ' ' ]

If you print that you'll get the 'ab efghij lmnopqrst ' you want :)

share|improve this answer
Nice explanation, but you are using a schlemiel the painter's algorithm which is highly inefficient if the list grows. Rather chose the solution form Norbert using #join: or #streamContents:. –  camillobruni Jan 25 '13 at 9:51

Your Answer


