我目前正在做一个squeak-Smalltalk的练习,我想拆分一个字符串,它的行为有点像Java或python中的拆分函数,但几乎没有在互联网上找到任何东西。有什么建议吗?如何实现它?
谢谢大家
不幸的是,似乎没有 split
或 splitBy:
的消息(不像Pharo...),所以我自己实现了这样一个方法。
split: aSentence
|count str strArr |
str := aSentence.
count := ((aSentence select: [:a | a = $ ])) size.
strArr := OrderedCollection new.
[count >= 0]
whileTrue: [
strArr add: (str copyFrom: 1 to: (str indexOf: $ ifAbsent: [str size + 1]) - 1).
str := str copyFrom: ((str indexOf: $ ) + 1) to: (str size).
count := count - 1.
].
^strArr