Given any String like ‘Lorem ipsum dolor sit amet, consectetuer adipiscing elit’ I’d like to discard every word before sit. I’ve checked methods in String but not found very useful for this one. This is my try:
| phrase newPhrase |
phrase := 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit'.
newPhrase := phrase substrings.
phrase substrings do: [: word | word = 'sit' ifFalse: [ newPhrase := newPhrase allButFirst ] ifTrue: [ ^ nil ] ].
newPhrase joinUsing: String space
it is answering nil when evaluated in a Workspace and however there must be a clever way right?
The code:
returns nil when the word sit is found.
Note that ^ in Smalltalk always return from the enclosing method. It doesn’t break out of the
do:.Try this instead
To discard all words before sit:
You may also find these collection methods interesting: