Create account / Log in

substring issues

Issues with the Vassal engine.

Moderators: uckelman, Tim M

substring issues

Postby marktb1961 » January 1st, 2021, 2:04 pm

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.
Platform: Vassal 3.5.3, MacOS Big Sur on iMac (27", mid-2020)
User avatar
marktb1961
 
Posts: 439
Joined: October 4th, 2018, 2:36 pm
Location: Liverpool, England

Re: substring issues

Postby shilinski » January 1st, 2021, 10:12 pm

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).
shilinski
 
Posts: 223
Joined: December 22nd, 2007, 8:46 am
Location: Laurel, Maryland

Re: substring issues

Postby Flint1b » January 11th, 2021, 9:22 am

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?
User avatar
Flint1b
 
Posts: 464
Joined: May 19th, 2020, 12:27 am
Location: Colonia Agrippina

Re: substring issues

Postby uckelman » January 19th, 2021, 9:36 pm

I've adjusted the docs for substring to indicate that the end is exclusive. Expect that in 3.4.13.
User avatar
uckelman
Site Admin
 
Posts: 9227
Joined: December 10th, 2007, 9:48 am
Location: Durham, England

Re: substring issues

Postby LoadEffectiveAddress » January 20th, 2021, 12:24 am

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

Code: Select all
|a|b|c|d|
0 1 2 3 4


This way, substring(1, 3) makes a lot more sense when it returns "bc"
LoadEffectiveAddress
 
Posts: 10
Joined: August 14th, 2020, 11:28 pm

Re: substring issues

Postby LoadEffectiveAddress » January 20th, 2021, 1:24 am

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.
LoadEffectiveAddress
 
Posts: 10
Joined: August 14th, 2020, 11:28 pm


Return to Technical Support & Bugs

Who is online

Users browsing this forum: Bing [Bot] and 5 guests