Pangea Pangea - 1 year ago 94
Groovy Question

String.split not counting the last empty elements

Look at the following 2 examples. How to make sure the 2nd one prints 4 as well?

String s="a|b|c|d"
//prints 4

//prints 2

Answer Source

To keep the empty token, use split with limit = -1

println(s.split("\\|", -1).length)​
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download