Hi everybody,
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')
Thanks, Tim
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
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
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
Am 29.12.2005 um 22:24 schrieb David T. Lewis:
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. ' ']]
#do:separatedBy: is meant exactly for that. Also, Stream understands #space itself:
(String streamContents: [:stream | 'A series of words to be shuffled' substrings shuffled do: [:each | stream nextPutAll: each] separatedBy: [stream space]])
- Bert -
squeak-dev@lists.squeakfoundation.org