How does slice work?

G

Gaba Luschi

How does the slice method work? If I want to write:

x.slice(parameter 1, parameter 2)

what do the parameters specify in terms of what part of x (say it's a
sentence) the program slices?

Thanks!
 
G

Gaba Luschi

Thanks - I'm still a little bit confused. Where does the slice
begin/stop slicing?
 
M

Michael Baker

It selects the number of elements given by the second parameter,
starting at the index given by the first parameter.
 
J

John Feminella

You pick a starting point to slice and then a number of items to
slice. For example, if you write:

"0123456789".slice(i, size)

then you will slice starting after the `i`-th character, and for
`size` more characters, or until the end of the sequence, whichever
comes first. For example,

"0123456789".slice(5, 3)

will yield "567". We start at "5" because the i =3D 5th character is
"4", and "5" is right after that. Then we take size =3D 3 characters in
all, yielding "567".

By contrast, if you wrote,

"abcdefgh".slice(5, 10)

you will get "fgh", because we can't take any more characters after
"h" from the sequence.

~ jf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top