by Cattlesquat » January 31st, 2020, 6:36 pm
Yes! In a beanshell "expression", java string functions are available. So you would be looking for the str.length() and str.charAt(n) functions.
But I have found that I can't just pass in the property name directly, I have to use GetProperty.
So for example instead of e.g. LocationName.length() I find I need to use GetProperty("LocationName").length()
Examples of string voodoo I have successfully accomplished in Vassal:
#1 Here is an expression I have used to check if the current location is listed in the table of victory objectives spaces (and give me 1 or 0 as the result). In this case I have two different AP_Objectives tables (AP_Objectives_0 and AP_Objectives_1) and I pick which one based on the global Variant property:
If(GetProperty("AP_Objectives_"+Variant).contains(GetProperty("LocationName")),1,0)
#2 Here I take a LocationName in the format "Turn 17", delete everything that isn't a 0-9 digit, and parse back into an integer value (which in the example of "Turn 17" would give me 17)
Integer.parseInt(GetProperty("LocationName").replaceAll("[^0-9]",""))
I would have expected I could just say LocationName.length() or whatever, but for whatever reason I only got it to work once I started using GetProperty.
I hope this is helpful! It was hard-won knowledge since I couldn't find functioning examples anywhere!
Brian