Home > Software design >  Kotlin and Java string split do not work in the same way
Kotlin and Java string split do not work in the same way

Time:11-11

I am trying to get text from a string between square brackets. So a regex like this does the job in my case:

Java:

String str = new String("date[gt]")
str.split("(\\[)|(\\])");  // Returns ["date", "gt"]

Doing the same with Kotlin returns "date[gt]".

Are these two supposed to be different?

CodePudding user response:

The String argument of Java's split() is used as a regex.

In Kotlin, there are multiple overloads of split().

Most likely you're using the overload that takes a string, and this one actually considers the string as a literal delimiter. What you want is the overload that takes a Regex:

"date[gt]".split(Regex("(\\[)|(\\])"))

// or if you want a more readable regex without double escapes

"date[gt]".split(Regex("""\[|\]"""))

Note that you explicitly need to pass a Regex instance to make sure you use this overload.

That being said, the behaviour still won't be perfectly equivalent because Kotlin will not remove trailing empty strings from the result. Since you're marking ] as a delimiter, your example string has a match and will yield 3 parts: ["date", "gt", ""].

If you're sure your strings follow the same pattern, you can simply remove the trailing ] manually and you don't even need a regex:

val (date, condition) = "date[gt]".removeSuffix("]").split("[") // ["date", "gt"]

You can also use capturing groups instead, if you have more complex requirements (modify as you need):

val value = "date[gt]"
val regex = Regex("""([^\[] )\[(\w )\]""")

val result = regex.matchEntire(value)
if (result == null) {
    error("The value didn't match the regex")
}

// group 0 is the entire match "date[gt]"
val date = result.groupValues[1] // "date"
val condition = result.groupValues[2] // "gt"

// do something with the values
  • Related