Class StringUtils


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

      • CASE_INSENSITIVE_COMPARATOR

        public static final Comparator<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 Comparator<String> UCS_CODEPOINT_COMPARATOR
        Compares Strings based on UCS codepoint values.
    • Method Detail

      • equalsWithNull

        public static boolean equalsWithNull​(String s1,
                                             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​(CharSequence source,
                                     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 Comparator<String> getCaseInsensitiveComparator()
        Return a comparator that performs case-insensitive string comparisons.
        Returns:
        A comparator that performs case insensitive comparisons.
      • isEmpty

        public static boolean isEmpty​(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 Integer number​(String s,
                                     int radix)
      • longNumber

        public static Long longNumber​(String s)
      • longNumber

        public static Long longNumber​(String s,
                                      int radix)
      • replace

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

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

        public static int safeNumber​(String s)
      • safeNumber

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

        public static long safeLongNumber​(String s)
      • safeLongNumber

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

        public static int skipOver​(CharSequence src,
                                   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​(CharSequence src,
                                    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​(StringBuilder buf)
        Remove white space from the end of a string buffer.
        Parameters:
        buf - - String buffer to be trimmed.
      • trimStart

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

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

        public static String toXML​(String sSrc,
                                   StringUtils.ToXMLType eTargetType,
                                   String sOthers,
                                   char cRangeMin,
                                   char cRangeMax,
                                   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 String toXHTML​(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.