1. Introduction
Thymeleaf is a modern server-side Java template engine for web and standalone applications. The engine has multiple utility classes for objects such as String
, Calendar
, Boolean
, List
, Map
, Array
, Number
or Date
. In this article, we will present utility helpers for String
objects. The implementation of String
utility methods can be found in the official Thymeleaf GitHub Repository.
2. Available utility methods for String
objects
Thymeleaf comes with several methods to process String objects on the template.
Method | Purpose | Description |
---|---|---|
${#strings.toString(obj)}
|
This is a Null-safe toString() method | You will not get NullPointerException when obj is null |
${#strings.isEmpty(name)} ${#strings.arrayIsEmpty(nameArr)} ${#strings.listIsEmpty(nameList)} ${#strings.setIsEmpty(nameSet)}
|
Check whether a String is empty (or null). |
Also works with arrays, lists or sets |
${#strings.defaultString(text,default)} ${#strings.arrayDefaultString(textArr,default)} ${#strings.listDefaultString(textList,default)} ${#strings.setDefaultString(textSet,default)}
|
This method perform an 'isEmpty()' check on a String and return it if false, when String is empty the default value will be returned. |
Also works with arrays, lists or sets |
${#strings.contains(name,'ez')} ${#strings.containsIgnoreCase(name,'ez')}
|
Check if String contains another String |
Also works with arrays, lists or sets |
${#strings.startsWith(name,'Don')} ${#strings.endsWith(name,endingFragment)}
|
Check whether a String starts or ends with a fragment | Also works with arrays, lists or sets |
${#strings.indexOf(name,frag)} ${#strings.substring(name,3,5)} ${#strings.substringAfter(name,prefix)} ${#strings.substringBefore(name,suffix)} ${#strings.replace(name,'las','ler')}
|
Substring-related operations | Also works with arrays, lists or sets |
${#strings.prepend(str,prefix)} ${#strings.append(str,suffix)}
|
Add prefix or suffix to the String |
Also works with arrays, lists or sets |
${#strings.toUpperCase(name)} ${#strings.toLowerCase(name)}
|
Print String uppercased or lowercased |
Also works with arrays, lists or sets |
${#strings.arrayJoin(namesArray,',')} ${#strings.listJoin(namesList,',')} ${#strings.setJoin(namesSet,',')} ${#strings.arraySplit(namesStr,',')} ${#strings.listSplit(namesStr,',')} ${#strings.setSplit(namesStr,',')}
|
Split String to Array or join Array items to the String using specified separator |
|
${#strings.trim(str)}
|
Trim String - remove white speces from the beginning and end of a String |
|
${#strings.length(str)}
|
Compute String length |
|
${#strings.abbreviate(str,10)}
|
Abbreviate text making it have a maximum size of n. If text is bigger, it will be clipped and finished in "..." | |
${#strings.capitalize(str)} ${#strings.unCapitalize(str)}
|
Convert the first character to upper-case (and vice-versa) | |
${#strings.capitalizeWords(str)} ${#strings.capitalizeWords(str,delimiters)}
|
Convert the first character of every word to upper-case | |
${#strings.escapeXml(str)} ${#strings.escapeJava(str)} ${#strings.escapeJavaScript(str)} ${#strings.unescapeJava(str)} ${#strings.unescapeJavaScript(str)}
|
Escape and unescape the String |
Also works with arrays, lists or sets |
${#strings.equals(first, second)} ${#strings.equalsIgnoreCase(first, second)} ${#strings.concat(values...)} ${#strings.concatReplaceNulls(nullValue, values...)}
|
Null-safe comparison and concatenation | |
${#strings.randomAlphanumeric(count)}
|
Generate random String with given length |
3. Conclusion
In this article, we presented Thymeleaf utility methods for String
objects. Showing text on the generated template is the main functionality, no wonder then Thymeleaf comes with so many utilities for this kind of object.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}