Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all items in from of Text from Ordered collection

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 '
like image 370
Irfan Avatar asked Dec 27 '22 10:12

Irfan


2 Answers

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| 
    list 
        do [:each| stream nextPutAll: each ]
        separatedBy: [ stream nextPut: Character space ]
like image 173
Norbert Hartl Avatar answered Feb 23 '23 18:02

Norbert Hartl


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 :)

like image 27
mgarciaisaia Avatar answered Feb 23 '23 20:02

mgarciaisaia