I have a string an I would like to be able to take a substring from the middle of it. For example for the string 'abcdefg' I want to get 'cde' (but the exact start and stop could be arbitrary). I have found allButFirst:
and allButLast:
messages so I can do it in two steps like
str := 'abcdefg'
(str allButFirst: 2) allButLast: 2
But I was expecting there to be a single message. I also tried using from:to:
(which naively makes sense to me) but that doesn't work on the string class.
Is there a message in Pharo 9 Smalltalk that will take the start and stop or start and length for getting a substring?
You can use
'abcdefg' copyFrom: 3 to: 5
and get cde
.
Here is one more important piece of advice:
You can go to Main Menu -> Tool -> Finder. Then in the Finder window switch from selectors search to examples. Then in the search bar enter
'abcdefg' . 3 . 5 . 'cde'
These are the "4 elements" of the search. The first one is the receiver, the last one is the expected result, and the middle elements will be used as parameters. So if there is any method that does what you expect, you will see in among the results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With