优雅的方式删除字符串的最后一部分?

nes*_*983 7 smalltalk

在Smalltalk中,如果给出字符串'OneTwoThree',我想删除最后的'Three'部分,.e.,在Squeak方法查找符号中:'OneTwoThree' . 'Three' . 'OneTwo'.

我能想到的最好的是:

'OneTwoThree' allButLast: 'Three' size,

但它感觉不太Smalltalk-ish,因为它使用子串长度,而不是子串本身.你会怎么编码?

Luk*_*gli 8

'OneTwoThree' readStream upToAll: 'Three'
Run Code Online (Sandbox Code Playgroud)


Jan*_*šek 6

我通常使用#copyReplaceAll:with:方法,如果最后一个字符串在原始字符串中没有重复,当然:

'OneTwoThree' copyReplaceAll: 'Three' with: ''
Run Code Online (Sandbox Code Playgroud)