How to get all items from text from an 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 to use streams too and it gave me back this "ordered collection ('ab' 'efghij' 'lmnopqrst')"

I only need one text with

'abc efghij lmnopqrst '

      

+3


source to share


2 answers


In Pharo, you can do

Character space join: list

      



If connection: is not available and should work well, you can use the stream option

String streamContents: [:stream| 
    list 
        do [:each| stream nextPutAll: each ]
        separatedBy: [ stream nextPut: Character space ]

      

+6


source


Object

the class has defined a message #asString

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 it aStringsList

will be Array

String

, the return call #asString

in each of the aList

members.

If you want to combine all of them into one String

, you can use the #inject:into:

collection method instead #collect:

:

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

      

If you type this you get the 'ab efghij lmnopqrst '

one you want :)

+3


source







All Articles