How do I get Variable size

giorgio.politano's Avatar

giorgio.politano

17 Jul, 2014 06:17 PM

Hi,

How do I get the number of characters from a variable.

Example:
    #set($test1 = "ABCD")
    #set($test2 = "EFG")

What I would like to get is :
    4 for the $test1 variable since there are 4 characters
    3 for the $test2 variable since there are 3 characters

If this is not possible, is there a way I can get the first character of each variable so i would get :
    "A" for $test1
    "E" for $test2

  1. 1 Posted by giorgio.politan... on 17 Jul, 2014 06:43 PM

    giorgio.politano's Avatar

    i figured out another way you can close this ticket thanks.

  2. 2 Posted by Ryan Griffith on 17 Jul, 2014 07:37 PM

    Ryan Griffith's Avatar

    Hi,

    Because the variable is a Java String, you can utilize any of the class' methods.

    For instance, to obtain the length of the variable, you can use:

    #set($test1 = "ABCD")
    $test1.length()
    

    And, to get the first character, you can use:

    #set($test1 = "ABCD")
    $test1.charAt(0)
    

    Please let me know if you have any questions.

    Thanks!

  3. Ryan Griffith closed this discussion on 04 Aug, 2014 12:48 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac