# Sub-string extraction

Does Vassal support sub-string extraction? And if so, what would be an example of how to do it…?

Specifically, I want to query the user for a string \$A\$. However, I plan to use \$A\$ as a text label for a piece and due to size constraints, want to ensure that the displayed length of \$A\$ is <= 3.

So I want to extract a sub-string \$B\$ which is bounded by index values of 1 & 3 of \$A\$.
\$B\$ is what will be used as the actual property that is displayed on the piece.

Or in psuedo-code:
B = A(1,3)

If I wanted to be even fancier, I could first eliminate any prepended/appended spaces, but that’s my perfectionism talking… I suspect there is a way to do this also…

Back when I used to program, this kind of string manipulation was pretty common so I’m guessing that Vassal has the ability, but it’s not obvious how to do it… I already checked the Module Designer Guide…

So any guidance/example of this would be very helpful!

You can use all Java string methods in Beanshell. Easiest solution would be to create B as a Calculated Property (CP), with the value A.substring(0,3). You can certainly be fancier; just add more Java string methods before or after the substring() method (e.g., A.trim().substring(0,3) to trim the whitespace first).

Keep in mind that Beanshell expressions normally must be enclosed in braces {}; CPs are one of the exceptions (as they only accept Beanshell, not old-style expressions).

Thanks JR - I’ll give it a try!

Using this function in a CALCULATED PROPERTY returned an error.

However, in a DYNAMIC PROPERTY it worked fine…

Ah, whoops, I didn’t realize substring() fails if the end point is beyond the end of the string (in other words, if A is less than 3 characters). So, to work in a CP, you would need to ensure A is always at least 3 characters, or at least you modify it to be at least 3 characters before the substriing() call. The easiest way I see to do that is to add 3 spaces to the end after trimming: ``` (A.trim() + " ").substring(0,3) ``` (you can then add another .trim() to the end if you want to trim any of those excess spaces at the end back off again). Probably a good idea to do this even if you’re using a DP instead of a CP, as the DP will cause an error if the user enters less than 3 characters.