Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Talking a slice of a string in Pharo smalltalk

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?

like image 935
cts Avatar asked Jul 10 '21 04:07

cts


1 Answers

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.

like image 154
Uko Avatar answered Oct 05 '22 15:10

Uko