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 '
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 ]
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 String
s 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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With