Class Strings

Object
org.thymeleaf.expression.Strings

public final class Strings extends Object

Expression Object for performing String-related operations inside Thymeleaf Standard Expressions.

An object of this class is usually available in variable evaluation expressions with the name #strings.

Since:
1.0
Author:
Daniel Fernández, Bernard Le Roux
  • Constructor Details

    • Strings

      public Strings(Locale locale)
  • Method Details

    • toString

      public String toString(Object target)

      Performs a null-safe toString() operation.

      Parameters:
      target - the object on which toString will be executed
      Returns:
      the result of calling target.toString() if target is not null, null if target is null.
      Since:
      2.0.12
    • arrayToString

      public String[] arrayToString(Object[] target)

      Performs a null-safe toString() operation on each element of the array.

      Parameters:
      target - the array of objects on which toString will be executed
      Returns:
      for each element: the result of calling target.toString() if target is not null, null if target is null.
      Since:
      2.0.12
    • listToString

      public List<String> listToString(List<?> target)

      Performs a null-safe toString() operation on each element of the list.

      Parameters:
      target - the list of objects on which toString will be executed
      Returns:
      for each element: the result of calling target.toString() if target is not null, null if target is null.
      Since:
      2.0.12
    • setToString

      public Set<String> setToString(Set<?> target)

      Performs a null-safe toString() operation on each element of the set.

      Parameters:
      target - the set of objects on which toString will be executed
      Returns:
      for each element: the result of calling target.toString() if target is not null, null if target is null.
      Since:
      2.0.12
    • abbreviate

      public String abbreviate(Object target, int maxSize)
    • arrayAbbreviate

      public String[] arrayAbbreviate(Object[] target, int maxSize)
    • listAbbreviate

      public List<String> listAbbreviate(List<?> target, int maxSize)
    • setAbbreviate

      public Set<String> setAbbreviate(Set<?> target, int maxSize)
    • equals

      public Boolean equals(Object first, Object second)
      Parameters:
      first - first
      second - second
      Returns:
      the result
      Since:
      2.0.16
    • equalsIgnoreCase

      public Boolean equalsIgnoreCase(Object first, Object second)
      Parameters:
      first - first
      second - second
      Returns:
      the result
      Since:
      2.0.16
    • contains

      public Boolean contains(Object target, String fragment)
    • arrayContains

      public Boolean[] arrayContains(Object[] target, String fragment)
    • listContains

      public List<Boolean> listContains(List<?> target, String fragment)
    • setContains

      public Set<Boolean> setContains(Set<?> target, String fragment)
    • containsIgnoreCase

      public Boolean containsIgnoreCase(Object target, String fragment)
    • arrayContainsIgnoreCase

      public Boolean[] arrayContainsIgnoreCase(Object[] target, String fragment)
    • listContainsIgnoreCase

      public List<Boolean> listContainsIgnoreCase(List<?> target, String fragment)
    • setContainsIgnoreCase

      public Set<Boolean> setContainsIgnoreCase(Set<?> target, String fragment)
    • startsWith

      public Boolean startsWith(Object target, String prefix)
    • arrayStartsWith

      public Boolean[] arrayStartsWith(Object[] target, String prefix)
    • listStartsWith

      public List<Boolean> listStartsWith(List<?> target, String prefix)
    • setStartsWith

      public Set<Boolean> setStartsWith(Set<?> target, String prefix)
    • endsWith

      public Boolean endsWith(Object target, String suffix)
    • arrayEndsWith

      public Boolean[] arrayEndsWith(Object[] target, String suffix)
    • listEndsWith

      public List<Boolean> listEndsWith(List<?> target, String suffix)
    • setEndsWith

      public Set<Boolean> setEndsWith(Set<?> target, String suffix)
    • substring

      public String substring(Object target, int start, int end)
    • arraySubstring

      public String[] arraySubstring(Object[] target, int start, int end)
    • listSubstring

      public List<String> listSubstring(List<?> target, int start, int end)
    • setSubstring

      public Set<String> setSubstring(Set<?> target, int start, int end)
    • substring

      public String substring(Object target, int start)

      copy a part of target start beginIndex to the end of target. If non-String object, toString() will be called.

      Parameters:
      target - source of the copy.
      start - index where the copy start.
      Returns:
      part of target, or null if target is null.
      Since:
      1.1.2
    • arraySubstring

      public String[] arraySubstring(Object[] target, int start)

      copy a part of target start beginIndex to the end of target for all the elements in the target array. If non-String object, toString() will be called.

      Parameters:
      target - source of the copy.
      start - index where the copy start.
      Returns:
      part of target, or null if target is null.
      Since:
      1.1.2
    • listSubstring

      public List<String> listSubstring(List<?> target, int start)

      copy a part of target start beginIndex to the end of target for all the elements in the target list. If non-String object, toString() will be called.

      Parameters:
      target - source of the copy.
      start - index where the copy start.
      Returns:
      part of target, or null if target is null.
      Since:
      1.1.2
    • setSubstring

      public Set<String> setSubstring(Set<?> target, int start)

      copy a part of target start beginIndex to the end of target for all the elements in the target set. If non-String object, toString() will be called.

      Parameters:
      target - source of the copy.
      start - index where the copy start.
      Returns:
      part of target, or null if target is null.
      Since:
      1.1.2
    • substringAfter

      public String substringAfter(Object target, String substr)
    • arraySubstringAfter

      public String[] arraySubstringAfter(Object[] target, String substr)
    • listSubstringAfter

      public List<String> listSubstringAfter(List<?> target, String substr)
    • setSubstringAfter

      public Set<String> setSubstringAfter(Set<?> target, String substr)
    • substringBefore

      public String substringBefore(Object target, String substr)
    • arraySubstringBefore

      public String[] arraySubstringBefore(Object[] target, String substr)
    • listSubstringBefore

      public List<String> listSubstringBefore(List<?> target, String substr)
    • setSubstringBefore

      public Set<String> setSubstringBefore(Set<?> target, String substr)
    • prepend

      public String prepend(Object target, String prefix)
    • arrayPrepend

      public String[] arrayPrepend(Object[] target, String prefix)
    • listPrepend

      public List<String> listPrepend(List<?> target, String prefix)
    • setPrepend

      public Set<String> setPrepend(Set<?> target, String prefix)
    • repeat

      public String repeat(Object target, int times)
      Parameters:
      target - target
      times - times
      Returns:
      the result
      Since:
      2.1.0
    • append

      public String append(Object target, String suffix)
    • concat

      public String concat(Object... values)
      Parameters:
      values - values
      Returns:
      the result
      Since:
      2.0.16
    • concatReplaceNulls

      public String concatReplaceNulls(String nullValue, Object... values)
      Parameters:
      nullValue - nullValue
      values - values
      Returns:
      the result
      Since:
      2.0.16
    • arrayAppend

      public String[] arrayAppend(Object[] target, String suffix)
    • listAppend

      public List<String> listAppend(List<?> target, String suffix)
    • setAppend

      public Set<String> setAppend(Set<?> target, String suffix)
    • indexOf

      public Integer indexOf(Object target, String fragment)
    • arrayIndexOf

      public Integer[] arrayIndexOf(Object[] target, String fragment)
    • listIndexOf

      public List<Integer> listIndexOf(List<?> target, String fragment)
    • setIndexOf

      public Set<Integer> setIndexOf(Set<?> target, String fragment)
    • isEmpty

      public Boolean isEmpty(Object target)
    • arrayIsEmpty

      public Boolean[] arrayIsEmpty(Object[] target)
    • listIsEmpty

      public List<Boolean> listIsEmpty(List<?> target)
    • setIsEmpty

      public Set<Boolean> setIsEmpty(Set<?> target)
    • arrayJoin

      public String arrayJoin(Object[] stringArray, String separator)
    • listJoin

      public String listJoin(List<?> stringIter, String separator)
    • setJoin

      public String setJoin(Set<?> stringIter, String separator)
    • arraySplit

      public String[] arraySplit(Object target, String separator)
    • listSplit

      public List<String> listSplit(Object target, String separator)
    • setSplit

      public Set<String> setSplit(Object target, String separator)
    • length

      public Integer length(Object target)
    • arrayLength

      public Integer[] arrayLength(Object[] target)
    • listLength

      public List<Integer> listLength(List<?> target)
    • setLength

      public Set<Integer> setLength(Set<?> target)
    • replace

      public String replace(Object target, String before, String after)
    • arrayReplace

      public String[] arrayReplace(Object[] target, String before, String after)
    • listReplace

      public List<String> listReplace(List<?> target, String before, String after)
    • setReplace

      public Set<String> setReplace(Set<?> target, String before, String after)
    • multipleReplace

      public String multipleReplace(Object target, String[] before, String[] after)
    • arrayMultipleReplace

      public String[] arrayMultipleReplace(Object[] target, String[] before, String[] after)
    • listMultipleReplace

      public List<String> listMultipleReplace(List<?> target, String[] before, String[] after)
    • setMultipleReplace

      public Set<String> setMultipleReplace(Set<?> target, String[] before, String[] after)
    • toUpperCase

      public String toUpperCase(Object target)
    • arrayToUpperCase

      public String[] arrayToUpperCase(Object[] target)
    • listToUpperCase

      public List<String> listToUpperCase(List<?> target)
    • setToUpperCase

      public Set<String> setToUpperCase(Set<?> target)
    • toLowerCase

      public String toLowerCase(Object target)
    • arrayToLowerCase

      public String[] arrayToLowerCase(Object[] target)
    • listToLowerCase

      public List<String> listToLowerCase(List<?> target)
    • setToLowerCase

      public Set<String> setToLowerCase(Set<?> target)
    • trim

      public String trim(Object target)
    • arrayTrim

      public String[] arrayTrim(Object[] target)
    • listTrim

      public List<String> listTrim(List<?> target)
    • setTrim

      public Set<String> setTrim(Set<?> target)
    • capitalize

      public String capitalize(Object target)

      Convert the first letter of target to uppercase (title-case, in fact).

      Parameters:
      target - the String to be capitalized. If non-String object, toString() will be called.
      Returns:
      String the result of capitalizing the target.
      Since:
      1.1.2
    • arrayCapitalize

      public String[] arrayCapitalize(Object[] target)

      Convert the first letter into uppercase (title-case, in fact) for all the elements in the target array.

      Parameters:
      target - the array of Strings to be capitalized. If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • listCapitalize

      public List<String> listCapitalize(List<?> target)

      Convert the first letter into uppercase (title-case, in fact) for all the elements in the target list.

      Parameters:
      target - the list of Strings to be capitalized. If non-String objects, toString() will be called.
      Returns:
      a List with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • setCapitalize

      public Set<String> setCapitalize(Set<?> target)

      Convert the first letter into uppercase (title-case, in fact) for all the elements in the target set.

      Parameters:
      target - the set of Strings to be capitalized. If non-String objects, toString() will be called.
      Returns:
      a Set with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • unCapitalize

      public String unCapitalize(Object target)

      Convert the first letter of target to lowercase.

      Parameters:
      target - the String to be uncapitalized. If non-String object, toString() will be called.
      Returns:
      String the result of uncapitalizing the target.
      Since:
      1.1.2
    • arrayUnCapitalize

      public String[] arrayUnCapitalize(Object[] target)

      Convert the first letter into lowercase for all the elements in the target array.

      Parameters:
      target - the array of Strings to be uncapitalized. If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of uncapitalizing each element of the target.
      Since:
      1.1.2
    • listUnCapitalize

      public List<String> listUnCapitalize(List<?> target)

      Convert the first letter into lowercase for all the elements in the target list.

      Parameters:
      target - the list of Strings to be uncapitalized. If non-String objects, toString() will be called.
      Returns:
      a List with the result of uncapitalizing each element of the target.
      Since:
      1.1.2
    • setUnCapitalize

      public Set<String> setUnCapitalize(Set<?> target)

      Convert the first letter into lowercase for all the elements in the target set.

      Parameters:
      target - the set of Strings to be uncapitalized. If non-String objects, toString() will be called.
      Returns:
      a Set with the result of uncapitalizing each element of the target.
      Since:
      1.1.2
    • capitalizeWords

      public String capitalizeWords(Object target)

      Convert the first letter of each words of target to uppercase (title-case, in fact). The default delimiter characters between the words are the whitespace characters (see Characters.IsWhiteSpace method in the Java documentation).

      Parameters:
      target - the String to be capitalized. If non-String object, toString() will be called.
      Returns:
      String the result of capitalizing the target.
      Since:
      1.1.2
    • arrayCapitalizeWords

      public String[] arrayCapitalizeWords(Object[] target)

      Convert the first letter of each words of target to uppercase (title-case, in fact) for all the elements in the target array. The default delimiter characters between the words are the whitespace characters (see Characters.IsWhiteSpace method in the Java documentation).

      Parameters:
      target - the array of Strings to be capitalized. If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • listCapitalizeWords

      public List<String> listCapitalizeWords(List<?> target)

      Convert the first letter of each words of target to uppercase (title-case, in fact) for all the elements in the target list. The default delimiter characters between the words are the whitespace characters (see Characters.IsWhiteSpace method in the Java documentation).

      Parameters:
      target - the list of Strings to be capitalized. If non-String objects, toString() will be called.
      Returns:
      a List with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • setCapitalizeWords

      public Set<String> setCapitalizeWords(Set<?> target)

      Convert the first letter of each words of target to uppercase (title-case, in fact) for all the elements in the target set. The default delimiter characters between the words are the whitespace characters (see Characters.IsWhiteSpace method in the Java documentation).

      Parameters:
      target - the set of Strings to be capitalized. If non-String objects, toString() will be called.
      Returns:
      a Set with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • capitalizeWords

      public String capitalizeWords(Object target, Object delimiters)

      Convert the first letter of each words of target to uppercase (title-case, in fact), using the specified delimiter chars for determining word ends/starts.

      Parameters:
      target - the String to be capitalized. If non-String object, toString() will be called.
      delimiters - the delimiters of the words. If non-String object, toString() will be called.
      Returns:
      String the result of capitalizing the target.
      Since:
      1.1.2
    • arrayCapitalizeWords

      public String[] arrayCapitalizeWords(Object[] target, Object delimiters)

      Convert the first letter of each words of target to uppercase (title-case, in fact) for all the elements in the target array. The specified delimiter chars will be used for determining word ends/starts.

      Parameters:
      target - the array of Strings to be capitalized. If non-String objects, toString() will be called.
      delimiters - the delimiters of the words. If non-String object, toString() will be called.
      Returns:
      a String[] with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • listCapitalizeWords

      public List<String> listCapitalizeWords(List<?> target, Object delimiters)

      Convert the first letter of each words of target to uppercase (title-case, in fact) for all the elements in the target list. The specified delimiter chars will be used for determining word ends/starts.

      Parameters:
      target - the list of Strings to be capitalized. If non-String objects, toString() will be called.
      delimiters - the delimiters of the words. If non-String object, toString() will be called.
      Returns:
      a List with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • setCapitalizeWords

      public Set<String> setCapitalizeWords(Set<?> target, Object delimiters)

      Convert the first letter of each words of target to uppercase (title-case, in fact) for all the elements in the target set. The specified delimiter chars will be used for determining word ends/starts.

      Parameters:
      target - the set of Strings to be capitalized. If non-String objects, toString() will be called.
      delimiters - the delimiters of the words. If non-String object, toString()
      Returns:
      a Set with the result of capitalizing each element of the target.
      Since:
      1.1.2
    • escapeXml

      public String escapeXml(Object target)

      XML-escapes the specified text.

      Parameters:
      target - the text to be escaped
      Returns:
      the escaped text.
      Since:
      2.0.9
    • arrayEscapeXml

      public String[] arrayEscapeXml(Object[] target)

      XML-escapes all the elements in the target array.

      Parameters:
      target - the array of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of each each element of the target.
      Since:
      2.0.9
    • listEscapeXml

      public List<String> listEscapeXml(List<?> target)

      XML-escapes all the elements in the target list.

      Parameters:
      target - the list of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a List with the result of each each element of the target.
      Since:
      2.0.9
    • setEscapeXml

      public Set<String> setEscapeXml(Set<?> target)

      XML-escapes all the elements in the target set.

      Parameters:
      target - the list of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a Set with the result of each each element of the target.
      Since:
      2.0.9
    • escapeJavaScript

      public String escapeJavaScript(Object target)

      JavaScript-escapes the specified text.

      Parameters:
      target - the text to be escaped
      Returns:
      the escaped text.
      Since:
      2.0.11
    • arrayEscapeJavaScript

      public String[] arrayEscapeJavaScript(Object[] target)

      JavaScript-escapes all the elements in the target array.

      Parameters:
      target - the array of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of each each element of the target.
      Since:
      2.0.11
    • listEscapeJavaScript

      public List<String> listEscapeJavaScript(List<?> target)

      JavaScript-escapes all the elements in the target list.

      Parameters:
      target - the list of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a List with the result of each each element of the target.
      Since:
      2.0.11
    • setEscapeJavaScript

      public Set<String> setEscapeJavaScript(Set<?> target)

      JavaScript-escapes all the elements in the target set.

      Parameters:
      target - the list of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a Set with the result of each each element of the target.
      Since:
      2.0.11
    • unescapeJavaScript

      public String unescapeJavaScript(Object target)

      JavaScript-unescapes the specified text.

      Parameters:
      target - the text to be unescaped
      Returns:
      the unescaped text.
      Since:
      2.0.11
    • arrayUnescapeJavaScript

      public String[] arrayUnescapeJavaScript(Object[] target)

      JavaScript-unescapes all the elements in the target array.

      Parameters:
      target - the array of Strings to be unescaped. If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of each each element of the target.
      Since:
      2.0.11
    • listUnescapeJavaScript

      public List<String> listUnescapeJavaScript(List<?> target)

      JavaScript-unescapes all the elements in the target list.

      Parameters:
      target - the list of Strings to be unescaped. If non-String objects, toString() will be called.
      Returns:
      a List with the result of each each element of the target.
      Since:
      2.0.11
    • setUnescapeJavaScript

      public Set<String> setUnescapeJavaScript(Set<?> target)

      JavaScript-unescapes all the elements in the target set.

      Parameters:
      target - the list of Strings to be unescaped. If non-String objects, toString() will be called.
      Returns:
      a Set with the result of each each element of the target.
      Since:
      2.0.11
    • escapeJava

      public String escapeJava(Object target)

      Java-escapes the specified text.

      Parameters:
      target - the text to be escaped
      Returns:
      the escaped text.
      Since:
      2.0.11
    • arrayEscapeJava

      public String[] arrayEscapeJava(Object[] target)

      Java-escapes all the elements in the target array.

      Parameters:
      target - the array of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of each each element of the target.
      Since:
      2.0.11
    • listEscapeJava

      public List<String> listEscapeJava(List<?> target)

      Java-escapes all the elements in the target list.

      Parameters:
      target - the list of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a List with the result of each each element of the target.
      Since:
      2.0.11
    • setEscapeJava

      public Set<String> setEscapeJava(Set<?> target)

      Java-escapes all the elements in the target set.

      Parameters:
      target - the list of Strings to be escaped. If non-String objects, toString() will be called.
      Returns:
      a Set with the result of each each element of the target.
      Since:
      2.0.11
    • unescapeJava

      public String unescapeJava(Object target)

      Java-unescapes the specified text.

      Parameters:
      target - the text to be unescaped
      Returns:
      the unescaped text.
      Since:
      2.0.11
    • arrayUnescapeJava

      public String[] arrayUnescapeJava(Object[] target)

      Java-unescapes all the elements in the target array.

      Parameters:
      target - the array of Strings to be unescaped. If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of each each element of the target.
      Since:
      2.0.11
    • listUnescapeJava

      public List<String> listUnescapeJava(List<?> target)

      Java-unescapes all the elements in the target list.

      Parameters:
      target - the list of Strings to be unescaped. If non-String objects, toString() will be called.
      Returns:
      a List with the result of each each element of the target.
      Since:
      2.0.11
    • setUnescapeJava

      public Set<String> setUnescapeJava(Set<?> target)

      Java-unescapes all the elements in the target set.

      Parameters:
      target - the list of Strings to be unescaped. If non-String objects, toString() will be called.
      Returns:
      a Set with the result of each each element of the target.
      Since:
      2.0.11
    • randomAlphanumeric

      public String randomAlphanumeric(int count)

      Builds a random String using characters 0..9 and A..Z.

      Parameters:
      count - length of the generated String
      Returns:
      a random String
      Since:
      2.1.0
    • defaultString

      public String defaultString(Object target, Object defaultValue)

      Checks if target text is empty and uses either target, or if the target is empty uses defaultValue.

      Parameters:
      target - value that to be checked if is null or empty If non-String objects, toString() will be called.
      defaultValue - value to use if target is empty If non-String objects, toString() will be called.
      Returns:
      either target, or if the target is empty defaultValue
      Since:
      2.1.3
    • arrayDefaultString

      public String[] arrayDefaultString(Object[] target, Object defaultValue)

      Checks if each target element is empty and uses either target element, or if the target element is empty uses defaultValue.

      Parameters:
      target - the array of values that to be checked if is null or empty If non-String objects, toString() will be called.
      defaultValue - value to return if target is empty If non-String objects, toString() will be called.
      Returns:
      a String[] with the result of defaultString(Object, Object) for each element of the target.
      Since:
      2.1.3
    • listDefaultString

      public List<String> listDefaultString(List<?> target, Object defaultValue)

      Checks if each target element is empty and uses either target element, or if the target element is empty uses defaultValue.

      Parameters:
      target - the list of values that to be checked if is null or empty If non-String objects, toString() will be called.
      defaultValue - value to return if target is empty If non-String objects, toString() will be called.
      Returns:
      a List<String> with the result of defaultString(Object, Object) for each element of the target.
      Since:
      2.1.3
    • setDefaultString

      public Set<String> setDefaultString(Set<?> target, Object defaultValue)

      Checks if each target element is empty and uses either target element, or if the target element is empty uses defaultValue.

      Parameters:
      target - the set of values that to be checked if is null or empty If non-String objects, toString() will be called.
      defaultValue - value to return if target is empty If non-String objects, toString() will be called.
      Returns:
      a Set<String> with the result of defaultString(Object, Object) for each element of the target.
      Since:
      2.1.3