Class StringUtil


  • public class StringUtil
    extends java.lang.Object
    Some assorted utility methods for dealing with Strings.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String LONG_TIME_FORMAT
      Format string used for long time expressions which include hours.
      static java.lang.String SHORT_TIME_FORMAT
      Format string used for short time expressions which are less than an hour.
      static java.lang.String SHORTER_TIME_FORMAT
      Format string used for short time expressions which are less than ten minutes.
    • Constructor Summary

      Constructors 
      Constructor Description
      StringUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isEmpty​(java.lang.String str)
      Determines if given string is empty or null.
      static java.lang.String join​(java.lang.String[] stringsToJoin, java.lang.String separator)
      Joins the Strings in the given array together into a single String using the specified separator String.
      static java.lang.String join​(java.util.List<java.lang.String> stringsToJoin, java.lang.String separator)
      Joins the Strings in the given List together into a single String using the specified separator String.
      static java.lang.CharSequence replaceAll​(java.lang.CharSequence template, java.lang.String[] sources, java.lang.CharSequence[] destinations)
      This method is based on Android's TextUtils.replace(CharSequence, String[], CharSequence[]) and extends its functionality by replacing all instances of each String specified in the sources parameter with the associated destination CharSequence.
      static java.lang.String stringForTime​(long milliseconds)
      Formats the given time duration value (in milliseconds) as a time string in one of the following formats: HH:MM:SS where the timeframe is greater than 1 hour MM:SS where the timeframe is less than an hour M:SS where the timeframe is less than ten minutes
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LONG_TIME_FORMAT

        public static final java.lang.String LONG_TIME_FORMAT
        Format string used for long time expressions which include hours.
        See Also:
        Constant Field Values
      • SHORT_TIME_FORMAT

        public static final java.lang.String SHORT_TIME_FORMAT
        Format string used for short time expressions which are less than an hour.
        See Also:
        Constant Field Values
      • SHORTER_TIME_FORMAT

        public static final java.lang.String SHORTER_TIME_FORMAT
        Format string used for short time expressions which are less than ten minutes.
        See Also:
        Constant Field Values
    • Constructor Detail

      • StringUtil

        public StringUtil()
    • Method Detail

      • isEmpty

        public static boolean isEmpty​(java.lang.String str)
        Determines if given string is empty or null.
        Parameters:
        str - the String to evaluate
        Returns:
        true if the String is null or contains only whitespace, false otherwise
      • join

        public static java.lang.String join​(java.util.List<java.lang.String> stringsToJoin,
                                            java.lang.String separator)
        Joins the Strings in the given List together into a single String using the specified separator String.
        Parameters:
        stringsToJoin - the List of Strings to join
        separator - the String to be inserted between each element of the array
        Returns:
        the concatenated String
      • stringForTime

        public static java.lang.String stringForTime​(long milliseconds)
        Formats the given time duration value (in milliseconds) as a time string in one of the following formats:
        • HH:MM:SS where the timeframe is greater than 1 hour
        • MM:SS where the timeframe is less than an hour
        • M:SS where the timeframe is less than ten minutes
        Parameters:
        milliseconds - the time duration to format, in milliseconds
        Returns:
        the formatted string expression for the given time duration
      • join

        public static java.lang.String join​(java.lang.String[] stringsToJoin,
                                            java.lang.String separator)
        Joins the Strings in the given array together into a single String using the specified separator String.
        Parameters:
        stringsToJoin - the array of Strings to join
        separator - the String to be inserted between each element of the array
        Returns:
        the concatenated String
      • replaceAll

        public static java.lang.CharSequence replaceAll​(java.lang.CharSequence template,
                                                        java.lang.String[] sources,
                                                        java.lang.CharSequence[] destinations)
        This method is based on Android's TextUtils.replace(CharSequence, String[], CharSequence[]) and extends its functionality by replacing all instances of each String specified in the sources parameter with the associated destination CharSequence. It is important to note that this exists for CharSequences (which are not necessarily Strings) that have Spanned portions and for which the relevant spans must be maintained. If we were to use String.replaceAll(String, String) in cases such as these, the span information would be lost.
        Parameters:
        template - the CharSequence on which we should perform replacements
        sources - an Array of Strings to find and replace in template
        destinations - an Array of CharSequences corresponding to the sources strings which will serve as replacements
        Returns:
        the CharSequence with all replacements applied
        See Also:
        TextUtils.replace(CharSequence, String[], CharSequence[])