简体   繁体   中英

Does javascript not have bracket notation slicing? I.e. string[0:4]

I have been trying to find an answer to this, searched 'bracket notation string' but could not find anything. Say I have:

let s = 'apple'

I know I can access any letter with s[x] but can you slice a string this way using javascript. Ie

s[0:3] 

to return "appl" ?

Use Array.prototype.slice() to obtain slices from an array:

let s = 'apple'

let mySlice = s.slice(0,4)
console.log(mySlice) // appl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM