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 Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
     
    (package private) static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Deprecated.
    TODO Make private in 4.0.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static boolean
    checkLaterThan1(CharSequence cs, CharSequence searchChar, int len2, int start1)
     
    (package private) 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.
    (package private) static int
    indexOf(CharSequence cs, CharSequence searchChar, int start)
    Used by the indexOf(CharSequence methods) as a green implementation of indexOf.
    (package private) 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.
    (package private) static int
    lastIndexOf(CharSequence cs, CharSequence searchChar, int start)
    Used by the lastIndexOf(CharSequence methods) as a green implementation of lastIndexOf
    (package private) static boolean
    regionMatches(CharSequence cs, boolean ignoreCase, int thisStart, CharSequence substring, int start, int length)
    Green implementation of regionMatches.
    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.
    static char[]
    Converts the given CharSequence to a char[].

    Methods inherited from class java.lang.Object

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

  • 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)
    • indexOf

      static int indexOf(CharSequence cs, CharSequence searchChar, int start)
      Used by the indexOf(CharSequence methods) as a green implementation of indexOf.
      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
    • 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)
      Green implementation of regionMatches.
      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
    • 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 - 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