Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to remove the last part of a string?

Tags:

smalltalk

In Smalltalk, if given the string 'OneTwoThree', I'd like to remove the last 'Three' part, .e., In Squeak method finder notation: 'OneTwoThree' . 'Three' . 'OneTwo'.

The best I can come up with is:

'OneTwoThree' allButLast: 'Three' size,

but it doesn't feel very Smalltalk-ish, because it uses the substring length, rather than the substring itself. How would you code it?

like image 579
nes1983 Avatar asked Jun 16 '10 13:06

nes1983


2 Answers

'OneTwoThree' readStream upToAll: 'Three'
like image 92
Lukas Renggli Avatar answered Oct 02 '22 13:10

Lukas Renggli


I usually use #copyReplaceAll:with: method, if the last string is not repeated elsewhere in the original string of course:

'OneTwoThree' copyReplaceAll: 'Three' with: ''
like image 27
Janko Mivšek Avatar answered Oct 02 '22 14:10

Janko Mivšek