How to split string into 2 parts after certain position

后端 未结 6 1565
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 10:53

For example I have some random string:

str = \"26723462345\"

And I want to split it in 2 parts after 6-th char. How to do this correctly?

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-17 11:18

    Here's a reusable version for you:

    str       = "26723462345"
    n         = str.length
    boundary  = 6
    head      = str.slice(0, boundary) # => "267234" 
    tail      = str.slice(boundary, n) # => "62345" 
    

    It also preserves the original string, which may come in handy later in the program.

提交回复
热议问题