'Groovy domain index is not bound while splitting an array

I have created very simple function that should return the first array after splitting it with a char.

int add(String version){
    splitVersion = version.split('.')
    echo splitVersion[0]
}

I'm calling the function like that: add("1.0.0")

And right after that I"m receving this error:

java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0

It's seem to be that it's not acting like a String at all, and when I try to print version it's print the value that sent(1.0.0)

What could be the issue?

Thanks!



Solution 1:[1]

The split method receives a regex, so you should use it like this.

int add(String version){
   splitVersion = version.split("\\.")
   echo splitVersion[0]
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ryuk