Skip to main content
Kinetic Community

Text Helper

  1. Getting Started
    1. Configuration
    2. Usage
  2. Methods
    1. contains(String string, String substring)
    2. containsRegex(String string, String regex)
    3. defaultIfBlank(String string, String defaultString)
    4. displaySize(long size)
    5. downcase(String string) downcase(String string, Locale locale)
    6. endsWith(String string, String substring)
    7. equals(String string1, String string2)
    8. equalsIgnoreCase(String string1, String string2)
    9. escape(String string)
    10. escapeJs(String string)
    11. escapeUrlParameter(String string)
    12. escapeUrlSegment(String string)
    13. isAlpha(String string)
    14. isAlphaSpace(String string)
    15. isAlphanumeric(String string)
    16. isAlphanumericSpace(String string)
    17. isBlank(String string)
    18. isEmpty(String string)
    19. isInteger(String string)
    20. isNotAlpha(String string)
    21. isNotAlphaSpace(String string)
    22. isNotAlphanumeric(String string)
    23. isNotAlphanumericSpace(String string)
    24. isNotBlank(String string)
    25. isNotEmpty(String string)
    26. isNotInteger(String string)
    27. isNotNull(String string)
    28. isNull(String string)
    29. join(Object[] objects) join(Object[] objects, String separator) join(Object[] objects, String separator, String conjunction) join(Collection objects) join(Collection objects, String separator) join(Collection objects, String separator, String conjunction) join(Map objects, String entrySeparator, String separator) join(Map objects, String entrySeparator, String separator, String conjunction)
    30. length(String string)
    31. matchesRegex(String string, String regex)
    32. padEnd(String string, int length, char paddingCharacter)
    33. padStart(String string, int length, char paddingCharacter)
    34. replace(String string, String substring, String replacement)
    35. replaceRegex(String string, String regex, String replacement)
    36. split(String string, String separator) split(String string, String separator, int limit)
    37. splitRegex(String string, String separator) splitRegex(String string, String separator, int limit)
    38. startsWith(String string, String substring)
    39. titlelize(String string)
    40. trim(String string) trim(String string, String defaultString)
    41. truncate(String string, int length) truncate(String string, int length, String suffix)
    42. upcase(String string) upcase(String string, Locale locale)
  3. Related

The Text Helper contains many useful methods for using and manipulating text within a bundle. All of the methods are designed to be “null safe”, which means that they will not throw any NullPointerException errors if null values are passed in as parameters. See the examples for each method to determine the result of passing in a null parameter.

There are two classes that share the same methods:

  • com.kineticdata.bundles.TextHelper must be initialized in the bundle/initialization.jspf file and can then be used in JSTL expressions.
  • com.kineticdata.bundles.Text is a static class that can be used in JSP scriptlets and declarations (this is primarily useful in custom bundle helper declarations).

Getting Started

Configuration

In order to use the text helper in a bundle, it must be loaded in the bundle/initialization.jspf file as shown below

<% request.setAttribute("text", new com.kineticdata.bundles.TextHelper()); %>

For a complete bundle/initialization.jspf example, see the default bundle implementation athttps://github.com/kineticdata/request-ce-bundle-default.

Usage

<%-- partials/sample.jsp --%>
<c:if test="${text.endsWith(user.email, '@acme.com')}">
  ${text.escape(user.displayname)}
</c:if>
<%-- bundle/BundleHelper.jspf --%>
<%!
  public static class BundleHelper {
    public String formatUsername(String username) {
      return com.kineticdata.bundles.Text.padEnd(
        com.kineticdata.bundles.Text.downcase(username), 25, '.');
    }
  }
%>

Methods

contains(String string, String substring)

Returns a boolean value indicating whether the specified string contains the substring.

text.contains(null, "bcd")    // false
text.contains("bcd", null)    // false
text.contains("", "")         // true
text.contains("abcde", "")    // true
text.contains("abcde", "bcd") // true
text.contains("abcde", "xyz") // false

containsRegex(String string, String regex)

Returns a boolean value indicating whether the specified string contains at least one regex match.

text.containsRegex(null, ".+")      // false
text.containsRegex("ab\nde", null)  // false
text.containsRegex("", "")          // true
text.containsRegex("ab\nde", "b?d") // true
text.containsRegex("ab\nde", "b?d") // true
text.containsRegex("abcde", "x?z")  // false

defaultIfBlank(String string, String defaultString)

Returns the string parameter if it is not blank (null, empty string, or whitespace), otherwise returns the defaultString parameter.

text.defaultIfBlank("abcde", "xyz") // "abcde"
text.defaultIfBlank(" ", "xyz")     // "xyz"
text.defaultIfBlank("", "xyz")      // "xyz"
text.defaultIfBlank(null, "xyz")    // "xyz"

displaySize(long size)

Returns the english, human-readable version of the specific number of bytes.

text.displaySize(1)    // "1 bytes"
text.displaySize(1000) // "1000 bytes"
text.displaySize(1024) // "1 KB"

downcase(String string)
downcase(String string, Locale locale)

Returns the null-safe conversion of a String to upper case as per String.toLowerCase() or String.toLowerCase(Locale).

text.downcase(null)               // null
text.downcase("abc")              // "abc"
text.downcase("AbC")              // "abc"
text.downcase("I", englishLocale) // "i"
text.downcase("I", turkishLocale) // "ı"

endsWith(String string, String substring)

Returns a null-safe boolean evaluation indicating whether the specified string ends with the substring.

text.endsWith(null, null)      // false
text.endsWith(null, "def")     // false
text.endsWith("abc", null)     // false
text.endsWith("", "abc")       // false
text.endsWith("abc", "")       // true
text.endsWith("abcdef", "def") // true
text.endsWith("abcdef", "xyz") // false

equals(String string1, String string2)

Returns a null-safe boolean evaluation indicating whether the two strings are equal.

text.equals(null, null)   // true
text.equals(null, "abc")  // false
text.equals("abc", null)  // false
text.equals("abc", "abc") // true
text.equals("abc", "ABC") // false

equalsIgnoreCase(String string1, String string2)

Returns a null-safe boolean evaluation indicating whether the two strings are equal (ignoring case).

text.equalsIgnoreCase(null, null)   // true
text.equalsIgnoreCase(null, "abc")  // false
text.equalsIgnoreCase("abc", null)  // false
text.equalsIgnoreCase("abc", "abc") // true
text.equalsIgnoreCase("abc", "ABC") // true

escape(String string)

Returns the specified string escaped in such as way that it can safely be included in HTML content (namely that all double quotes, less than signs, greater than signs, and ampersands are escaped).

text.escape("'\"<>&") // "'&quot;&lt;&gt;&amp;"

escapeJs(String string)

Returns the specified string escaped in such a way that it can be safely included in JavaScript content (namely that all single quotes, double quotes, and backslashes are escaped).

text.escapeJs("'\"/") // "\'\"\/"

escapeUrlParameter(String string)

Returns the specified string escaped in such a way that it can be safely included in URL form parameter names and values.

text.escapeUrlParameter("abc.-_~@:!$&'()*+,;= Ä\"") // "abc.-_%7E%40%3A%21%24%26%27%28%29*%2B%2C%3B%3D+%C3%84%22"

escapeUrlSegment(String string)

Returns the specified string escaped in such a way that it can be safely included in URL path segment.

text.escapeUrlSegment("abc.-_~@:!$&'()*+,;= Ä\"") // "abc.-_~@:!$&'()*+,;=%20%C3%84%22"

isAlpha(String string)

Returns true if the String contains only unicode letters.

If the string is null, false will be returned. If the string is empty, true will be returned.

text.isAlpha(null)      // false
text.isAlpha("")        // true
text.isAlpha("abc")     // true
text.isAlpha("!abc")    // false
text.isAlpha("abc abc") // false
text.isAlpha("abc123")  // false
text.isAlpha("abc 123") // false

isAlphaSpace(String string)

Returns true if the String contains only unicode letters and space characters.

If the string is null, false will be returned. If the string is empty, true will be returned.

text.isAlphaSpace(null)      // false
text.isAlphaSpace("")        // true
text.isAlphaSpace("abc")     // true
text.isAlphaSpace("!abc")    // false
text.isAlphaSpace("abc abc") // true
text.isAlphaSpace("abc123")  // false
text.isAlphaSpace("abc 123") // false

isAlphanumeric(String string)

Returns true if the String contains only unicode letters and digits.

If the string is null, false will be returned. If the string is empty, true will be returned.

text.isAlphanumeric(null)      // false
text.isAlphanumeric("")        // true
text.isAlphanumeric("abc")     // true
text.isAlphanumeric("!abc")    // false
text.isAlphanumeric("abc abc") // false
text.isAlphanumeric("abc123")  // true
text.isAlphanumeric("abc 123") // false

isAlphanumericSpace(String string)

Returns true if the String contains only unicode letters, digits, and space characters.

If the string is null, false will be returned. If the string is empty, true will be returned.

text.isAlphanumericSpace(null)      // false
text.isAlphanumericSpace("")        // true
text.isAlphanumericSpace("abc")     // true
text.isAlphanumericSpace("!abc")    // false
text.isAlphanumericSpace("abc abc") // true
text.isAlphanumericSpace("abc123")  // true
text.isAlphanumericSpace("abc 123") // true

isBlank(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is blank (is null, the empty string, or a string of whitespace).

text.isBlank(null)  // true
text.isBlank("")    // true
text.isBlank(" ")   // true
text.isBlank("abc") // false

isEmpty(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is empty (is null or the empty string).

text.isEmpty(null)  // true
text.isEmpty("")    // true
text.isEmpty(" ")   // false
text.isEmpty("abc") // false

isInteger(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is an integer.

text.isInteger(null)  // false
text.isInteger("")    // false
text.isInteger("1")   // true
text.isInteger("a")   // false
text.isInteger("1.1") // false

isNotAlpha(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is not comprised soley of alpha characters.

text.isNotAlpha(null)      // true
text.isNotAlpha("")        // false
text.isNotAlpha("abc")     // false
text.isNotAlpha("!abc")    // true
text.isNotAlpha("abc abc") // true
text.isNotAlpha("abc123")  // true
text.isNotAlpha("abc 123") // true

isNotAlphaSpace(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is not comprised soley of alpha characters and spaces.

text.isNotAlphaSpace(null)      // true
text.isNotAlphaSpace("")        // false
text.isNotAlphaSpace("abc")     // false
text.isNotAlphaSpace("!abc")    // true
text.isNotAlphaSpace("abc abc") // false
text.isNotAlphaSpace("abc123")  // true
text.isNotAlphaSpace("abc 123") // true

isNotAlphanumeric(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is not comprised soley of alphanumeric characters.

text.isNotAlphanumeric(null)      // true
text.isNotAlphanumeric("")        // false
text.isNotAlphanumeric("abc")     // false
text.isNotAlphanumeric("!abc")    // true
text.isNotAlphanumeric("abc abc") // true
text.isNotAlphanumeric("abc123")  // false
text.isNotAlphanumeric("abc 123") // true

isNotAlphanumericSpace(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is not comprised soley of alphanumeric characters and spaces.

text.isNotAlphanumericSpace(null)      // true
text.isNotAlphanumericSpace("")        // false
text.isNotAlphanumericSpace("abc")     // false
text.isNotAlphanumericSpace("!abc")    // true
text.isNotAlphanumericSpace("abc abc") // false
text.isNotAlphanumericSpace("abc123")  // false
text.isNotAlphanumericSpace("abc 123") // false

isNotBlank(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is not empty (is not null, the empty string, or a string of whitespace).

text.isNotBlank(null)  // false
text.isNotBlank("")    // false
text.isNotBlank(" ")   // false
text.isNotBlank("abc") // true

isNotEmpty(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is not empty (is not null or the empty string).

text.isNotEmpty(null)  // false
text.isNotEmpty("")    // false
text.isNotEmpty(" ")   // true
text.isNotEmpty("abc") // true

isNotInteger(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is not an integer.

text.isNotInteger(null)  // true
text.isNotInteger("")    // true
text.isNotInteger("1")   // false
text.isNotInteger("a")   // true
text.isNotInteger("1.1") // true

isNotNull(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is not null.

text.isNotNull(null)  // false
text.isNotNull("")    // true
text.isNotNull(" ")   // true
text.isNotNull("abc") // true

isNull(String string)

Returns a null-safe boolean evaluation indicating whether the provided string is null.

text.isNull(null)  // true
text.isNull("")    // false
text.isNull(" ")   // false
text.isNull("abc") // false

join(Object[] objects)
join(Object[] objects, String separator)
join(Object[] objects, String separator, String conjunction)
join(Collection objects)
join(Collection objects, String separator)
join(Collection objects, String separator, String conjunction)
join(Map objects, String entrySeparator, String separator)
join(Map objects, String entrySeparator, String separator, String conjunction)

Joins the elements of the provided collection into a single String.

text.join(["a","b","c"])                               // "abc"
text.join(["a","b","c"], ", ")                         // "a, b, c"
text.join(["a"],", ","and")                            // "a"
text.join(["a","b"],", ","and")                        // "a and b"
text.join(["a","b","c"],", ","and")                    // "a, b, and c"
text.join({"a":"A","b":"B","c":"C"}, ":", ", ")        // "a:A, b:B, c:C"
text.join({"a":"A"}, ":", ", ", "and")                 // "a:A"
text.join({"a":"A","b":"B"}, ":", ", ", "and")         // "a:A and b:B"
text.join({"a":"A","b":"B","c":"C"}, ":", ", ", "and") // "a:A, b:B, and c:C"

length(String string)

Null-safe length operation (the length of “” and null are both 0).

text.length(null)  // 0
text.length("")    // 0
text.length("abc") // 3

matchesRegex(String string, String regex)

Returns a null-safe boolean evaluation indicating whether the entire string matches the specified regex.

text.matchesRegex(null, ".*")        // false
text.matchesRegex("", ".*")          // true
text.matchesRegex("abcdefg", "a.+g") // true
text.matchesRegex("abcdefg", "b.+g") // false

padEnd(String string, int length, char paddingCharacter)

Pads the end of the specified string with a padding character.

text.padEnd(null, 5, '.')  // "....."
text.padEnd("", 5, '.')    // "....."
text.padEnd("abc", 5, '.') // "abc.."

padStart(String string, int length, char paddingCharacter)

Pads the beginning of the specified string with a padding character.

text.padStart(null, 5, '.')  // "....."
text.padStart("", 5, '.')    // "....."
text.padStart("abc", 5, '.') // "..abc"

replace(String string, String substring, String replacement)

Replaces all occurrences of a substring within the specified string.

text.replace(null, "def", "xyz")        // null
text.replace("abcdef", "def", "xyz")    // "abcxyz"
text.replace("abcdef", "def", "$1")     // "abc$1"
text.replace("abcdef", "d(.+)f", "xyz") // "abcdef"
text.replace("abcdef", "d(.+)f", "$1")  // "abcdef"

replaceRegex(String string, String regex, String replacement)

Replaces all substrings matching the specified regex within the specified string.

text.replaceRegex(null, "d(.+)f", "xyz")     // null
text.replaceRegex("abcdef", "def", "xyz")    // "abcxyz"
text.replaceRegex("abcdef", "def", "\$1")   // "abc$1"
text.replaceRegex("abcdef", "d(.+)f", "xyz") // "abcxyz"
text.replaceRegex("abcdef", "d(.+)f", "$1")  // "abce"

split(String string, String separator)
split(String string, String separator, int limit)

Returns a list of substrings split by the specified separator string. If the limit parameter is passed, than the resulting list will contain up to the specified value entries. If the limit parameter is negative, the string will be split from the end to the beginning until the limit is reached.

text.split(null, ",")        // []
text.split(",", ",")         // []
text.split("a1b2c", "\\d")   // ["a1b2c"]
text.split("a|b|c", "|")     // ["a", "b", "c"]
text.split("a|b|c", "|", 2)  // ["a", "b|c"]
text.split("a|b|c", "|", -2) // ["a|b", "c"]

splitRegex(String string, String separator)
splitRegex(String string, String separator, int limit)

Returns a list of substrings split by the specified separator regex. If the limit parameter is passed, than the resulting list will contain up to the specified value entries. If the limit parameter is negative, the string will be split from the end to the beginning until the limit is reached.

text.split(null, ",")          // []
text.split(",", ",")           // []
text.split("a1b2c", "\\d")     // ["a", "b", "c"]
text.split("a|b|c", "\\|")     // ["a", "b", "c"]
text.split("a|b|c", "\\|", 2)  // ["a", "b|c"]
text.split("a|b|c", "\\|", -2) // ["a|b", "c"]

startsWith(String string, String substring)

Returns a boolean value indicating whether the specified string starts with the substring.

text.startsWith(null, null)      // false
text.startsWith(null, "abc")     // false
text.startsWith("abc", null)     // false
text.startsWith("", "abc")       // false
text.startsWith("abcdef", "")    // true
text.startsWith("abcdef", "abc") // true
text.startsWith("abcdef", "xyz") // false

substring(String string, int offset)
substring(String string, int offset, int length)

Returns a the remainder of the string after the offset. A negative offset indicates an offset relative to the end of the string. This method will not return an IndexOutOfBoundsException if the length is greater than the string length.

text.substring("First and Last", 0)      // "First and Last"
text.substring("First and Last", 10)     // "Last"
text.substring("First and Last", -4)     // "Last"
text.substring("First and Last", 0, 5)   // "First"
text.substring("First and Last", 0, 100) // "First and Last"
text.substring("First and Last", -4, 4)  // "Last"

titlelize(String string)

Capitalizes all of the words in the string (a word is considered to be a sequence of alpha-numeric characters separated by a space or dash).

text.titlelize("Pride And Prejudice") // "Pride And Prejudice"
text.titlelize("pride and prejudice") // "Pride And Prejudice"
text.titlelize("Pride-and_prejudice") // "Pride-And_prejudice"

trim(String string)
trim(String string, String defaultString)

Returns the string with leading and trailing whitespace removed. If the defaultString parameter is specified, it will be returned instead if the string is blank (null, empty string, or all whitespace).

text.trim(null)           // null
text.trim("")             // ""
text.trim(" ")            // ""
text.trim("abc")          // "abc"
text.trim(" abc ")        // "abc"
text.trim("abc", "def")   // "abc"
text.trim(" abc ", "def") // "abc"
text.trim(" ", "def")     // "def"

truncate(String string, int length)
truncate(String string, int length, String suffix)

Returns the string truncated to the specified length. If the suffix parameter is specified, it will be appended to the end of the truncated string (the length of the suffix counts towards the overall length of the truncated String).

text.truncate(null, 5)               // null
text.truncate("123456789", 5)        // "12345"
text.truncate(null, 5, "...")        // null
text.truncate("123456789", 5, "...") // "12..."

upcase(String string)
upcase(String string, Locale locale)

Returns the null-safe conversion of a String to upper case as per String.toUpperCase() or String.toUpperCase(Locale).

text.upcase(null)               // null
text.upcase("ABC")              // "ABC"
text.upcase("aBc")              // "ABC"
text.upcase("i", englishLocale) // "I"
text.upcase("ı", turkishLocale) // "I"

Related