substring issues

Vassal v3.4.12

Attempt to use substring fails when the object string is length less than the specified 2nd parameter in the substring expression:
Example error generated:- Expression evaluation error {name.substring(0,21)}
In the example, this workaround resolves the issue: {name.substring(0,Math.min(name.length(),21))}

Also, when the 1st parameter is non-zero, the substring returns null. Observed when trying to use the following example…
Role.substring(1,1) where Role was a dynamic property of various lengths >2. Role.substring(0,1) worked fine in the same example.

FYI, there is an omission in the Vassal doc. For string.substring(x,y), y is exclusive, which means it is your end character index + 1. I looked up substring in the java docs to find this out. For example for dog = “doggy”, to get “dog”, you need substring(0,3).

This is probably working as intended.

substring() needing to be told to return a possibly shorter string if the source string is not long enough, is pretty common. The solution is to dynamically decide the 2nd parameter to substring(), like in the workaround.

The second problem, does it return null or the empty string? Is it possibly a problem with substring(a, b) and a=b which according to the docs and shilinski’s comment would always return the empty string? In the given example, would Role.substring(1, 2) work?

I’ve adjusted the docs for substring to indicate that the end is exclusive. Expect that in 3.4.13.

I find it helps to think of the index as pointing to the spaces between characters, rather than a particular character.

|a|b|c|d| 0 1 2 3 4

This way, substring(1, 3) makes a lot more sense when it returns “bc”

Sorry for my bizarre-seeming comment - when I viewed the thread it was scrolled perfectly to show shilinski’s post at the top of the window. I thought that was the first post in the thread, so I was just replying to that.