String Functions

Setting a variable string

The code below demonstrates how you to set a variable and then output it.

The $! preceding the variable name means if the variable is set.

The brackets represent where the variable begins and ends - for example you should use the brackets if you need to use the variable in a url http://www.url.co.uk/${site}/article/

  1. #set ($variableName = "Hello World!")  
  2.   
  3. $!{variableName}  

String - Contains

This can be be used with check boxes, radio boxes and drop downs. (These items tend to add a comma to the content).

The example below demonstrates how you can grab the URL of the page and then see if it contains a certain item.

  1. #if ($url.toLowerCase().contains('www.url.co.uk/jobs/'))  
  2.       You are looking at the jobs page!  
  3. #end  

String - Change Case

A useful method if you are obtaining a variable from the querystring and are worried that its been typed in the wrong format.

You can also use the css syntax text-transform with any of the following values: none, capitalize - (first character of each word is uppercase), uppercase, lowercase, inherit.

  1. ## Retrieve value from the querystring  
  2. #set ($example = $request.getParameter('example-string-value') )  
  3.   
  4. #set ($example = "AmEriCA")  
  5.   
  6. ## Output AMERICA  
  7. $example.toUpperCase()  
  8.   
  9. ## Output america  
  10. $example.toLowerCase()  

String - Equals

To be used with input fields etc. Be aware not to use with check boxes, radio boxes and drop downs because these items add a comma to the content unless you remove the comma. In that instance its better to use the .contains() method

  1. #if($term.equals('Fall'))  

String - Escape

Use the code below to remove unnecessary html code, prepare a string for javascript and other useful methods.

  1. #set ($title = "<p>James is awesome</p>")  
  2. $!{esc.xml($title)}  
  3.   
  4. $javascript                  -> He didn't say, "Stop!"  
  5. $esc.javascript($javascript) -> He didn\'t say, \"Stop!\"  
  6.   
  7. $html                        -> "bread" & "butter"  
  8. $esc.html($html)            -> &quot;bread&quot; &amp; &quot;butter&quot;  
  9.   
  10. $xml                        -> "bread" & "butter"  
  11. $esc.xml($xml)              -> &quot;bread&quot; &amp; &quot;butter&quot;  
  12.   
  13. Other methods include  
  14.   
  15. java() - Escapes the characters in a String using Java String rules.  
  16. javascript() - Escapes the characters in a String using JavaScript String rules.  
  17. html() - Escapes the characters in a String using HTML entities.  
  18. url() - Escapes the characters in a String to be suitable to use as an HTTP parameter value.  
  19. xml() - Escapes the characters in a String using XML entities.  
  20. sql() - Escapes the characters in a String to be suitable to pass to an SQL query.  
  21. propertyKey() - Escapes the characters in a String using the rules for writing Java properties.  
  22. propertyValue() - Escapes the characters in a String using the rules for Java properties.  
  23. getDollar() - Renders a dollar sign ($).  
  24. getD() - Renders a dollar sign ($).  
  25. getHash() - Renders a hash (#).  
  26. getH() - Renders a hash (#).  
  27. getBackslash() - Renders a backslash (\).  
  28. getB() - Renders a backslash (\).  
  29. getQuote() - Renders a double quotation mark (").  
  30. getQ() - Renders a double quotation mark (").  
  31. getSingleQuote() - Renders a single quotation mark (').  
  32. getS() - Renders a single quotation mark (').  
  33. getExclamation() - Renders an exclamation mark (!).  
  34. getE() - Renders an exclamation mark (!).  

String - Hide if null

If the string is null then nothing will display.

  1. $!{stringName}  

String - isSet()

You can generally use the example directly above if its just for a variable. This is useful when you need to write HTML based on a variable. See example:.

  1. #if($UtilMethods.isSet($item.shortTitle))  
  2.       <li>${content.shortTitle}</li>  
  3. #else  
  4.       <li>${content.title}</li>  
  5. #end  

String - Mathematics

How to add, subtract, multiply and divide:

  1. #set ($value = $a + 1) ## Addition  
  2. #set ($value = $b - 1) ## Subtraction  
  3. #set ($value = $a * $b) ## Multiplication  
  4. #set ($value = $a / $b) ## Division  

String - Remove the last character of a string

If you need to remove the last character of a string then you can use the following little snippet, probably rarely used but incredibly useful when you need it.

  1. #set ($string = "/feeds/url/?type=1&type=2&q=$!{keyword}&")  
  2. #set ($stringLength = $string.length() - 1)  
  3. #set ($string = $string.substring(0,$stringLength))  
  4. $string  

String - Replace

To replace a character in a string (in this instance hyphen) use the following :

  1. #set($key = "south-america")  
  2. $key.replaceAll("-"" ")  
  3. $key  
  4.   
  5. <span style="text-transform:capitalize;"> $!{key} </span>  

$key will output "south america", you can always use text-transform as shown on line 4 above using either capitalize (Transforms the first character of each word to uppercase), uppercase (Transforms all characters to uppercase) or lowercase (Transforms all characters to lowercase). You can also use

To remove all html tags:

  1. #set($mystring = "<span>test span</span><p> test p</p>")  
  2. $mystring.replaceAll("</?[^>]+/?>""")  

String - Trim

Use the code below to remove unnecessary white space at the beginning or the end of a string.

  1. #set ($title = "James is awesome    ")  
  2. $title.trim()  
comments powered by Disqus