Class StringUtils


  • public final class StringUtils
    extends java.lang.Object
    This class provides some utility methods that were available in jfString, but are not part of the Java String interface.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  StringUtils.ToXMLType  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Comparator<java.lang.String> CASE_INSENSITIVE_COMPARATOR
      Publicly available case-insensitive comparator.
      static java.util.Comparator<java.lang.String> UCS_CODEPOINT_COMPARATOR
      Compares Strings based on UCS codepoint values.
      static java.lang.String WHITE_SPACE
      Set of characters considered to be white space.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean equalsWithNull​(java.lang.String s1, java.lang.String s2)
      General-purpose string equality comparison for strings that may be null.
      static int findNoCase​(java.lang.CharSequence source, java.lang.CharSequence target, int fromIndex)
      Scan a string for a string, but ignore case.
      static java.util.Comparator<java.lang.String> getCaseInsensitiveComparator()
      Return a comparator that performs case-insensitive string comparisons.
      static boolean isEmpty​(java.lang.CharSequence s)
      Empty string test.
      static java.lang.Long longNumber​(java.lang.String s)  
      static java.lang.Long longNumber​(java.lang.String s, int radix)  
      static java.lang.Integer number​(java.lang.String s)  
      static java.lang.Integer number​(java.lang.String s, int radix)  
      static java.lang.String parseToken​(java.lang.StringBuilder source)  
      static void replace​(java.lang.StringBuilder buf, char find, char rep)  
      static void replace​(java.lang.StringBuilder buf, char find, char rep, int start, int limit)  
      static long safeLongNumber​(java.lang.String s)  
      static long safeLongNumber​(java.lang.String s, int radix)  
      static int safeNumber​(java.lang.String s)  
      static int safeNumber​(java.lang.String s, int radix)  
      static int skipOver​(java.lang.CharSequence src, java.lang.String sSkip, int nOffset)
      Scan this string for the first character not in the given set.
      static int skipUntil​(java.lang.CharSequence src, java.lang.String sSkip, int nOffset)
      Scan this string for the first character in the given set.
      static java.lang.String toXHTML​(java.lang.String sSrc, boolean bIncludePI)
      Replaces specified characters in the current string with their entity references.
      static java.lang.String toXML​(java.lang.String src, boolean isAttribute)  
      static java.lang.String toXML​(java.lang.String sSrc, StringUtils.ToXMLType eTargetType, java.lang.String sOthers, char cRangeMin, char cRangeMax, java.lang.String sExclude)
      Replaces specified characters in the current string with their entity references.
      static java.lang.String trim​(java.lang.String s)  
      static void trim​(java.lang.StringBuilder buf)
      Remove white space from the end of a string buffer.
      static java.lang.String trimStart​(java.lang.String s)  
      static void trimStart​(java.lang.StringBuilder buf)
      Remove white space from the start of a string buffer.
      • Methods inherited from class java.lang.Object

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

      • CASE_INSENSITIVE_COMPARATOR

        public static final java.util.Comparator<java.lang.String> CASE_INSENSITIVE_COMPARATOR
        Publicly available case-insensitive comparator. You can also use the static method getCaseInsensitiveComparator() to get one.
      • UCS_CODEPOINT_COMPARATOR

        public static final java.util.Comparator<java.lang.String> UCS_CODEPOINT_COMPARATOR
        Compares Strings based on UCS codepoint values.
      • WHITE_SPACE

        public static final java.lang.String WHITE_SPACE
        Set of characters considered to be white space.
        See Also:
        Constant Field Values
    • Method Detail

      • equalsWithNull

        public static boolean equalsWithNull​(java.lang.String s1,
                                             java.lang.String s2)
        General-purpose string equality comparison for strings that may be null. This method compares two strings for equality, allowing for possible null references and non-interned strings.
        Parameters:
        s1 - First string to compare.
        s2 - First string to compare.
        Returns:
        True if the strings are equal; false if not. Note that two null references would be considered equal.
      • findNoCase

        public static int findNoCase​(java.lang.CharSequence source,
                                     java.lang.CharSequence target,
                                     int fromIndex)
        Scan a string for a string, but ignore case.

        The only ASCII case folding is considered. Comparisons are done on a character by character basis (i.e., Unicode not code points are not considered).

        Parameters:
        source - the string being searched.
        target - the string to search for.
        fromIndex - the index to begin searching from.
        Returns:
        the start position for where the string was found. If the string was not found, -1.
      • getCaseInsensitiveComparator

        public static java.util.Comparator<java.lang.String> getCaseInsensitiveComparator()
        Return a comparator that performs case-insensitive string comparisons.
        Returns:
        A comparator that performs case insensitive comparisons.
      • isEmpty

        public static boolean isEmpty​(java.lang.CharSequence s)
        Empty string test. Short-hand to test a string for null pointer or empty condition.
        Parameters:
        s - String to be tested.
        Returns:
        True if the given string reference is null or empty; false otherwise.
      • number

        public static java.lang.Integer number​(java.lang.String s)
      • number

        public static java.lang.Integer number​(java.lang.String s,
                                               int radix)
      • longNumber

        public static java.lang.Long longNumber​(java.lang.String s)
      • longNumber

        public static java.lang.Long longNumber​(java.lang.String s,
                                                int radix)
      • parseToken

        public static java.lang.String parseToken​(java.lang.StringBuilder source)
      • replace

        public static void replace​(java.lang.StringBuilder buf,
                                   char find,
                                   char rep)
      • replace

        public static void replace​(java.lang.StringBuilder buf,
                                   char find,
                                   char rep,
                                   int start,
                                   int limit)
      • safeNumber

        public static int safeNumber​(java.lang.String s)
      • safeNumber

        public static int safeNumber​(java.lang.String s,
                                     int radix)
      • safeLongNumber

        public static long safeLongNumber​(java.lang.String s)
      • safeLongNumber

        public static long safeLongNumber​(java.lang.String s,
                                          int radix)
      • skipOver

        public static int skipOver​(java.lang.CharSequence src,
                                   java.lang.String sSkip,
                                   int nOffset)
        Scan this string for the first character not in the given set. Similar to strspn().
        Parameters:
        sSkip - - the characters to scan for
        nOffset - - the position where to start the scan. Default = 0
        Returns:
        The position, relative to nOffset, for the first character not found in the given set
      • skipUntil

        public static int skipUntil​(java.lang.CharSequence src,
                                    java.lang.String sSkip,
                                    int nOffset)
        Scan this string for the first character in the given set. Similar to strcspn().
        Parameters:
        src - - the string to scan
        sSkip - - the characters to scan for
        nOffset - - the position where to start the scan. Default = 0.
        Returns:
        The position, relative to nOffset, for the first character found in the given set
      • trim

        public static void trim​(java.lang.StringBuilder buf)
        Remove white space from the end of a string buffer.
        Parameters:
        buf - - String buffer to be trimmed.
      • trimStart

        public static void trimStart​(java.lang.StringBuilder buf)
        Remove white space from the start of a string buffer.
        Parameters:
        buf - - String buffer to be trimmed.
      • trim

        public static java.lang.String trim​(java.lang.String s)
      • trimStart

        public static java.lang.String trimStart​(java.lang.String s)
      • toXML

        public static java.lang.String toXML​(java.lang.String src,
                                             boolean isAttribute)
      • toXML

        public static java.lang.String toXML​(java.lang.String sSrc,
                                             StringUtils.ToXMLType eTargetType,
                                             java.lang.String sOthers,
                                             char cRangeMin,
                                             char cRangeMax,
                                             java.lang.String sExclude)
        Replaces specified characters in the current string with their entity references.
          '&' is replaced with "&amp;"
          '<' is replaced with "&lt;"
          if eTargetType = XMLTEXT
              '>' is replaced with "&gt;"
          if eTargetType = XMLATTRIBUTE_WITH_DQUOTE
              '"' is replaced with "&quot;"
          if eTargetType = XMLATTRIBUTE_WITH_QUOTE
              ''' is replaced with "'"
         
        Any characters found in sOthers are replaced with entity references. Any characters found inbetween and including the cRangeStart and cRangeEnd are replaced with entity references.
        Parameters:
        sSrc - the source string.
        eTargetType - an enum indicating the target xml type.
        sOthers - a list of characters to be encoded in this string.
        cRangeMin - Any characters greater than or equal to this char are encoded with their entity references.
        cRangeMax - Any characters less than or equal to this char are encoded with their entity references.
        sExclude - a list of characters NOT to be encoded in this string.
        Returns:
        The converted string.
      • toXHTML

        public static java.lang.String toXHTML​(java.lang.String sSrc,
                                               boolean bIncludePI)
        Replaces specified characters in the current string with their entity references. and wraps the text in and

        elements.

          '&' is replaced with "&amp;"
          '<' is replaced with "&lt;"
          '>' is replaced with "&gt;"
         
        All line feeds are are replaced with
        .
        Parameters:
        sSrc - the source string.
        bIncludePI - true if the string should be prefixed with .
        Returns:
        The converted string.