Search code examples
stringmethodssubstringsmalltalkpharo

Extract substring using index in Pharo Smalltalk


I'm trying to get a substring from an initial string in Smalltalk. I'm wondering if there's a way to do it. For example in Java, the method aStringObject.substring(index), allows you to trim a String object using an index (or its position in the array). I've been looking in the browser for something that works in a similar way, but couldn't find it. So far every trimming method uses a character or string to do the separation.

As an example of what I'm looking for:

initialString:='Hello'.
finalString:=initialString substring: 1

The value of finalString should be 'ello'.


Solution

  • In Smalltalk a String is a type of SequencableCollection so you can use the copying protocol messages as well.

    For example you could use:

    • copyFrom: start to: stop
    • allButFirst (will not copy the first character)
    • allButFirst: n (more generally answer a copy of the receiver containing all but the first n elements.