Enum StringHelpers

  • All Implemented Interfaces:
    Helper<java.lang.Object>, java.io.Serializable, java.lang.Comparable<StringHelpers>

    public enum StringHelpers
    extends java.lang.Enum<StringHelpers>
    implements Helper<java.lang.Object>
    Commons string function helpers.
    Since:
    0.2.2
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      abbreviate
      Truncates a string if it is longer than the specified number of characters.
      capitalize
      Capitalizes all the whitespace separated words in a String.
      capitalizeFirst
      Capitalizes the first character of the value.
      center
      Centers the value in a field of a given width.
      cut
      Removes all values of arg from the given string.
      dateFormat
      Usage:
      defaultIfEmpty
      If value evaluates to False, uses the given default.
      join
      Joins an array, iterator or an iterable with a string.
      ljust
      Left-aligns the value in a field of a given width.
      lower
      Converts a string into all lowercase.
      now
      Usage:
      numberFormat
      Usage:
      replace
      Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
      rjust
      Right-aligns the value in a field of a given width.
      slugify
      Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens.
      stringFormat
      Formats the variable according to the argument, a string formatting specifier.
      stripTags
      Strips all [X]HTML tags.
      substring
      Returns a new CharSequence that is a subsequence of this sequence.
      upper
      Converts a string into all uppercase.
      wordWrap
      Wraps words at specified line length.
      yesno
      Maps values for true, false and (optionally) null, to the strings "yes", "no", "maybe".
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object apply​(java.lang.Object context, Options options)
      Apply the helper to the context.
      static void register​(Handlebars handlebars)
      Register all the text helpers.
      void registerHelper​(Handlebars handlebars)
      Register the helper in a handlebars instance.
      static StringHelpers valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static StringHelpers[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        compareTo, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

      • capitalizeFirst

        public static final StringHelpers capitalizeFirst
        Capitalizes the first character of the value. For example:
         {{capitalizeFirst value}}
         
        If value is "handlebars.java", the output will be "Handlebars.java".
      • center

        public static final StringHelpers center
        Centers the value in a field of a given width. For example:
         {{center value size=19 [pad="char"] }}
         
        If value is "Handlebars.java", the output will be " Handlebars.java ".
      • cut

        public static final StringHelpers cut
        Removes all values of arg from the given string. For example:
         {{cut value [" "]}}
         
        If value is "String with spaces", the output will be "Stringwithspaces".
      • defaultIfEmpty

        public static final StringHelpers defaultIfEmpty
        If value evaluates to False, uses the given default. Otherwise, uses the value. For example:
         {{defaultIfEmpty value ["nothing"] }}
         If value is "" (the empty string), the output will be nothing.
         
      • join

        public static final StringHelpers join
        Joins an array, iterator or an iterable with a string. For example:
         {{join value " // " [prefix=""] [suffix=""]}}
         

        If value is the list ['a', 'b', 'c'], the output will be the string "a // b // c".

        Or:
         {{join "a" "b" "c" " // " [prefix=""] [suffix=""]}}
         Join the "a", "b", "c", the output will be the string "a // b // c".
         
      • ljust

        public static final StringHelpers ljust
        Left-aligns the value in a field of a given width. Argument: field size For example:
         {{ljust value 20 [pad=" "] }}
         
        If value is Handlebars.java, the output will be "Handlebars.java ".
      • rjust

        public static final StringHelpers rjust
        Right-aligns the value in a field of a given width. Argument: field size For example:
         {{rjust value 20 [pad=" "] }}
         
        If value is Handlebars.java, the output will be " Handlebars.java".
      • substring

        public static final StringHelpers substring
        Returns a new CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1 Argument: start offset end offset For example:
         {{substring value 11 }}
         
        If value is Handlebars.java, the output will be "java". or
         {{substring value 0 10 }}
         
        If value is Handlebars.java, the output will be "Handlebars".
      • lower

        public static final StringHelpers lower
        Converts a string into all lowercase. For example:
         {{lower value}}
         
        If value is 'Still MAD At Yoko', the output will be 'still mad at yoko'.
      • upper

        public static final StringHelpers upper
        Converts a string into all uppercase. For example:
         {{upper value}}
         
        If value is 'Hello', the output will be 'HELLO'.
      • slugify

        public static final StringHelpers slugify
        Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace. For example:
         {{slugify value}}
         
        If value is "Joel is a slug", the output will be "joel-is-a-slug".
      • stringFormat

        public static final StringHelpers stringFormat
        Formats the variable according to the argument, a string formatting specifier. For example:
         {{stringFormat string param0 param1 ... paramN}}
         
        If value is "Hello %s" "handlebars.java", the output will be "Hello handlebars.java".
        See Also:
        String.format(String, Object...)
      • stripTags

        public static final StringHelpers stripTags
        Strips all [X]HTML tags. For example:
         {{stripTags value}}
         
      • capitalize

        public static final StringHelpers capitalize
        Capitalizes all the whitespace separated words in a String. For example:
         {{ capitalize value [fully=false]}}
         
        If value is "my first post", the output will be "My First Post".
      • abbreviate

        public static final StringHelpers abbreviate
        Truncates a string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence ("..."). Argument: Number of characters to truncate to For example:
         {{abbreviate value 13 }}
         
        If value is "Handlebars rocks", the output will be "Handlebars...".
      • wordWrap

        public static final StringHelpers wordWrap
        Wraps words at specified line length. Argument: number of characters at which to wrap the text For example:
         {{ wordWrap value 5 }}
         
        If value is Joel is a slug, the output would be:
         Joel
         is a
         slug
         
      • replace

        public static final StringHelpers replace
        Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. For example:
         {{ replace value "..." "rocks" }}
         
        If value is "Handlebars ...", the output will be "Handlebars rocks".
      • yesno

        public static final StringHelpers yesno
        Maps values for true, false and (optionally) null, to the strings "yes", "no", "maybe". For example:
         {{yesno value [yes="yes"] [no="no"] maybe=["maybe"] }}
         
      • dateFormat

        public static final StringHelpers dateFormat

        Usage:

            {{dateFormat date ["format"] [format="format"][locale="locale"][tz=timeZone|timeZoneId]
                [time="format"]}}
         
        Format parameters is one of:
        • "full": full date format. For example: Tuesday, June 19, 2012
        • "long": long date format. For example: June 19, 2012
        • "medium": medium date format. For example: Jun 19, 2012
        • "short": short date format. For example: 6/19/12
        • "pattern": a DateTimeFormatter pattern.
        Otherwise, the default formatter will be used. The format option can be specified as a parameter or hash (a.k.a named parameter).
        • The "locale" parameter can be use to select a locale, e.g. "de" or "en_GB". It defaults to the system locale.
        • The "tz" parameter is the time zone to use, e.g. "Europe/Berlin" or "GMT-8:00". It defaults to the system time zone.
        • The "time" parameter specifies the format of the time part, it can be "full", "long", "medium" or "short". If you do not specify it only the date part will appear in the output string.
      • numberFormat

        public static final StringHelpers numberFormat

        Usage:

            {{numberFormat number ["format"] [locale=default]}}
         
        Format parameters is one of:
        • "integer": the integer number format
        • "percent": the percent number format
        • "currency": the decimal number format
        • "pattern": a decimal pattern.
        Otherwise, the default formatter will be used.

        More options:

        • groupingUsed: Set whether or not grouping will be used in this format.
        • maximumFractionDigits: Sets the maximum number of digits allowed in the fraction portion of a number.
        • maximumIntegerDigits: Sets the maximum number of digits allowed in the integer portion of a number
        • minimumFractionDigits: Sets the minimum number of digits allowed in the fraction portion of a number
        • minimumIntegerDigits: Sets the minimum number of digits allowed in the integer portion of a number.
        • parseIntegerOnly: Sets whether or not numbers should be parsed as integers only.
        • roundingMode: Sets the RoundingMode used in this NumberFormat.
        See Also:
        NumberFormat, DecimalFormat
      • now

        public static final StringHelpers now

        Usage:

            {{now ["format"] [tz=timeZone|timeZoneId]}}
         
        Format parameters is one of:
        • "full": full date format. For example: Tuesday, June 19, 2012
        • "long": long date format. For example: June 19, 2012
        • "medium": medium date format. For example: Jun 19, 2012
        • "short": short date format. For example: 6/19/12
        • "pattern": a date pattern.
        Otherwise, the default formatter will be used.
    • Method Detail

      • values

        public static StringHelpers[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (StringHelpers c : StringHelpers.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static StringHelpers valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • apply

        public java.lang.Object apply​(java.lang.Object context,
                                      Options options)
                               throws java.io.IOException
        Description copied from interface: Helper
        Apply the helper to the context.
        Specified by:
        apply in interface Helper<java.lang.Object>
        Parameters:
        context - The context object.
        options - The options object.
        Returns:
        A string result.
        Throws:
        java.io.IOException - If a template cannot be loaded.
      • registerHelper

        public void registerHelper​(Handlebars handlebars)
        Register the helper in a handlebars instance.
        Parameters:
        handlebars - A handlebars object. Required.
      • register

        public static void register​(Handlebars handlebars)
        Register all the text helpers.
        Parameters:
        handlebars - The helper's owner. Required.