Kotlin split with regex work not as expected

2019-06-24 07:12发布

问题:

I am trying to split string with chunks of 16 chars length. So first of all I create string with 64 length

val data = "Some string"
data = String.format("%-64s", data)

Then I split it with regex

 val nameArray = data.split(Regex("(?<=\\G.{16})").toPattern())

Here I expext to get 4 chunks with 16 chars, but I got only 2, where first is 16 and second is 48.

Where am I wrong here?

Kotlin 1.2.61, Oracle JDK 1.8.0_181-b13, Windows 10

回答1:

Here's how I split it with regex

.{16}

Note: I'm not sure what all the other stuff in there is trying to do, maybe string specific items?