Class CharSequenceUtils

java.lang.Object
org.apache.commons.lang3.CharSequenceUtils

public class CharSequenceUtils extends Object
Operations on CharSequence that are null safe.
Since:
3.0
See Also:
  • Field Details

    • NOT_FOUND

      private static final int NOT_FOUND
      See Also:
    • STRING_FOLDS_SUPPLEMENTARY_CASE

      private static final boolean STRING_FOLDS_SUPPLEMENTARY_CASE
      Whether the running JDK folds a supplementary code point split across a surrogate pair when comparing case insensitively in String.regionMatches(boolean, int, String, int, int). JDKs up to and including Java 11 compare surrogate by surrogate and never match such a pair; later JDKs fold the whole code point. Probing what String actually does (rather than gating on a version constant) keeps every CharSequence type in step with String on whatever JDK is running. DESERET CAPITAL LETTER LONG I (U+10400) folds to its small form (U+10428).
    • TO_STRING_LIMIT

      static final int TO_STRING_LIMIT
      See Also:
  • Constructor Details

    • CharSequenceUtils

      @Deprecated public CharSequenceUtils()
      Deprecated.
      TODO Make private in 4.0.
      CharSequenceUtils instances should NOT be constructed in standard programming.

      This constructor is public to permit tools that require a JavaBean instance to operate.

  • Method Details

    • checkLaterThan1

      private static boolean checkLaterThan1(CharSequence cs, CharSequence searchChar, int len2, int start1)
    • equalsIgnoreCase

      private static boolean equalsIgnoreCase(int cp1, int cp2)
      Tests whether two code points are equal ignoring case, matching the folding used by String.regionMatches(boolean, int, String, int, int).
      Parameters:
      cp1 - The first code point.
      cp2 - The second code point.
      Returns:
      whether the code points are equal ignoring case.
    • indexOf

      static int indexOf(CharSequence cs, CharSequence searchChar, int start)
      Used by the indexOf(CharSequence methods) as a green implementation of indexOf.

      CharSequence types without a dedicated branch are scanned in place rather than materialized with toString(): for builder types (for example org.apache.commons.lang3.text.StrBuilder), toString() copies the whole buffer, and callers that invoke this method once per occurrence (such as deleteAll/replaceAll) would multiply that copy into allocation-quadratic churn.

      Parameters:
      cs - The CharSequence to be processed.
      searchChar - The CharSequence to be searched for.
      start - The start index.
      Returns:
      The index where the search sequence was found, or -1 if there is no such occurrence.
    • indexOf

      static int indexOf(CharSequence cs, int searchChar, int start)
      Returns the index within cs of the first occurrence of the specified character, starting the search at the specified index.

      If a character with value searchChar occurs in the character sequence represented by the cs object at an index no smaller than start, then the index of the first such occurrence is returned. For values of searchChar in the range from 0 to 0xFFFF (inclusive), this is the smallest value k such that:

       (this.charAt(k) == searchChar) && (k >= start)
       

      is true. For other values of searchChar, it is the smallest value k such that:

       (this.codePointAt(k) == searchChar) && (k >= start)
       

      is true. In either case, if no such character occurs inm cs at or after position start, then -1 is returned.

      There is no restriction on the value of start. If it is negative, it has the same effect as if it were zero: the entire CharSequence may be searched. If it is greater than the length of cs, it has the same effect as if it were equal to the length of cs: -1 is returned.

      All indices are specified in char values (Unicode code units).

      Parameters:
      cs - The CharSequence to be processed, not null.
      searchChar - The char to be searched for.
      start - The start index, negative starts at the string start.
      Returns:
      The index where the search char was found, -1 if not found.
      Since:
      3.6 updated to behave more like String.
    • lastIndexOf

      static int lastIndexOf(CharSequence cs, CharSequence searchChar, int start)
      Used by the lastIndexOf(CharSequence methods) as a green implementation of lastIndexOf
      Parameters:
      cs - The CharSequence to be processed.
      searchChar - The CharSequence to find.
      start - The start index.
      Returns:
      The index where the search sequence was found.
    • lastIndexOf

      static int lastIndexOf(CharSequence cs, int searchChar, int start)
      Returns the index within cs of the last occurrence of the specified character, searching backward starting at the specified index. For values of searchChar in the range from 0 to 0xFFFF (inclusive), the index returned is the largest value k such that:
       (this.charAt(k) == searchChar) && (k <= start)
       

      is true. For other values of searchChar, it is the largest value k such that:

       (this.codePointAt(k) == searchChar) && (k <= start)
       

      is true. In either case, if no such character occurs in cs at or before position start, then -1 is returned.

      All indices are specified in char values (Unicode code units).

      Parameters:
      cs - The CharSequence to be processed.
      searchChar - The char to be searched for.
      start - The start index, negative returns -1, beyond length starts at end.
      Returns:
      The index where the search char was found, -1 if not found.
      Since:
      3.6 updated to behave more like String.
    • regionMatches

      static boolean regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length)
      Tests if two string regions are equal.
      Parameters:
      cs - The CharSequence to be processed.
      ignoreCase - whether or not to be case-insensitive.
      thisStart - The index to start on the cs CharSequence.
      substring - The CharSequence to be looked for.
      start - The index to start on the substring CharSequence.
      length - character length of the region.
      Returns:
      whether the region matched.
      See Also:
    • subSequence

      public static CharSequence subSequence(CharSequence cs, int start)
      Returns a new CharSequence that is a subsequence of this sequence starting with the char value at the specified index.

      This provides the CharSequence equivalent to String.substring(int). The length (in char) of the returned sequence is length() - start, so if start == end then an empty sequence is returned.

      Parameters:
      cs - The specified subsequence, null returns null.
      start - The start index, inclusive, valid.
      Returns:
      A new subsequence, may be null.
      Throws:
      IndexOutOfBoundsException - Thrown if start is negative or if start is greater than length().
    • toCharArray

      public static char[] toCharArray(CharSequence source)
      Converts the given CharSequence to a char[].
      Parameters:
      source - The CharSequence to be processed.
      Returns:
      The resulting char array, never null.
      Since:
      3.11