How to split string into 2 parts after certain position

后端 未结 6 1663
隐瞒了意图╮
隐瞒了意图╮ 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:22

    The best way IMO is string.scan(/.{6}/)

    irb(main)> str
    => "abcdefghijklmnopqrstuvwxyz"
    irb(main)> str.scan(/.{13}/)
    => ["abcdefghijklm", "nopqrstuvwxyz"]
    

提交回复
热议问题