Search code examples
javasubstringfreemarker

Getting the last four characters of a string in FreeMarker


I have a FreeMarker variable, ${string} which can be of any length. How can I get the last four characters only?

For example:

string = "326235253235235";

I want to display: "5235";


Solution

  • get the length of the string by string.length(); then use substring to identify where you need to trim from string like;

    string.substring(string.length()-4,string.length());
    

    substring method usage;

    string.substring(beginning index,end index);`
    
    so your beginning index is 326235253235235
                                          |
    
    
    
    and your end index is 326235253235235
                                        |