Squeak Smalltalk中的分割函数

问题描述 投票:1回答:1

我目前正在做一个squeak-Smalltalk的练习,我想拆分一个字符串,它的行为有点像Java或python中的拆分函数,但几乎没有在互联网上找到任何东西。有什么建议吗?如何实现它?

谢谢大家

string split smalltalk squeak
1个回答
0
投票

不幸的是,似乎没有 splitsplitBy: 的消息(不像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
© www.soinside.com 2019 - 2024. All rights reserved.