Class Chars
- java.lang.Object
- 
- com.google.common.primitives.Chars
 
- 
 @GwtCompatible(emulated=true) public final class Chars extends Object Static utility methods pertaining tocharprimitives, that are not already found in eitherCharacterorArrays.All the operations in this class treat charvalues strictly numerically; they are neither Unicode-aware nor locale-dependent.See the Guava User Guide article on primitive utilities. - Since:
- 1.0
- Author:
- Kevin Bourrillion
 
- 
- 
Field SummaryFields Modifier and Type Field Description static intBYTESThe number of bytes required to represent a primitivecharvalue.
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static List<Character>asList(char... backingArray)Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[]).static charcheckedCast(long value)Returns thecharvalue that is equal tovalue, if possible.static intcompare(char a, char b)Compares the two specifiedcharvalues.static char[]concat(char[]... arrays)Returns the values from each provided array combined into a single array.static charconstrainToRange(char value, char min, char max)Returns the value nearest tovaluewhich is within the closed range[min..max].static booleancontains(char[] array, char target)Returnstrueiftargetis present as an element anywhere inarray.static char[]ensureCapacity(char[] array, int minLength, int padding)Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length.static charfromByteArray(byte[] bytes)Returns thecharvalue whose big-endian representation is stored in the first 2 bytes ofbytes; equivalent toByteBuffer.wrap(bytes).getChar().static charfromBytes(byte b1, byte b2)Returns thecharvalue whose byte representation is the given 2 bytes, in big-endian order; equivalent toChars.fromByteArray(new byte[] {b1, b2}).static inthashCode(char value)Returns a hash code forvalue; equal to the result of invoking((Character) value).hashCode().static intindexOf(char[] array, char target)Returns the index of the first appearance of the valuetargetinarray.static intindexOf(char[] array, char[] target)Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.static Stringjoin(String separator, char... array)Returns a string containing the suppliedcharvalues separated byseparator.static intlastIndexOf(char[] array, char target)Returns the index of the last appearance of the valuetargetinarray.static Comparator<char[]>lexicographicalComparator()Returns a comparator that compares twochararrays lexicographically; not advisable for sorting user-visible strings as the ordering may not match the conventions of the user's locale.static charmax(char... array)Returns the greatest value present inarray.static charmin(char... array)Returns the least value present inarray.static voidreverse(char[] array)Reverses the elements ofarray.static voidreverse(char[] array, int fromIndex, int toIndex)Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive.static charsaturatedCast(long value)Returns thecharnearest in value tovalue.static voidsortDescending(char[] array)Sorts the elements ofarrayin descending order.static voidsortDescending(char[] array, int fromIndex, int toIndex)Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order.static char[]toArray(Collection<Character> collection)Copies a collection ofCharacterinstances into a new array of primitivecharvalues.static byte[]toByteArray(char value)Returns a big-endian representation ofvaluein a 2-element byte array; equivalent toByteBuffer.allocate(2).putChar(value).array().
 
- 
- 
- 
Field Detail- 
BYTESpublic static final int BYTES The number of bytes required to represent a primitivecharvalue.Java 8 users: use Character.BYTESinstead.- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
hashCodepublic static int hashCode(char value) Returns a hash code forvalue; equal to the result of invoking((Character) value).hashCode().Java 8 users: use Character.hashCode(char)instead.- Parameters:
- value- a primitive- charvalue
- Returns:
- a hash code for the value
 
 - 
checkedCastpublic static char checkedCast(long value) Returns thecharvalue that is equal tovalue, if possible.- Parameters:
- value- any value in the range of the- chartype
- Returns:
- the charvalue that equalsvalue
- Throws:
- IllegalArgumentException- if- valueis greater than- Character.MAX_VALUEor less than- Character.MIN_VALUE
 
 - 
saturatedCastpublic static char saturatedCast(long value) Returns thecharnearest in value tovalue.- Parameters:
- value- any- longvalue
- Returns:
- the same value cast to charif it is in the range of thechartype,Character.MAX_VALUEif it is too large, orCharacter.MIN_VALUEif it is too small
 
 - 
comparepublic static int compare(char a, char b) Compares the two specifiedcharvalues. The sign of the value returned is the same as that of((Character) a).compareTo(b).Note for Java 7 and later: this method should be treated as deprecated; use the equivalent Character.compare(char, char)method instead.- Parameters:
- a- the first- charto compare
- b- the second- charto compare
- Returns:
- a negative value if ais less thanb; a positive value ifais greater thanb; or zero if they are equal
 
 - 
containspublic static boolean contains(char[] array, char target) Returnstrueiftargetis present as an element anywhere inarray.- Parameters:
- array- an array of- charvalues, possibly empty
- target- a primitive- charvalue
- Returns:
- trueif- array[i] == targetfor some value of- i
 
 - 
indexOfpublic static int indexOf(char[] array, char target) Returns the index of the first appearance of the valuetargetinarray.- Parameters:
- array- an array of- charvalues, possibly empty
- target- a primitive- charvalue
- Returns:
- the least index ifor whicharray[i] == target, or-1if no such index exists.
 
 - 
indexOfpublic static int indexOf(char[] array, char[] target) Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.More formally, returns the lowest index isuch thatArrays.copyOfRange(array, i, i + target.length)contains exactly the same elements astarget.- Parameters:
- array- the array to search for the sequence- target
- target- the array to search for as a sub-sequence of- array
 
 - 
lastIndexOfpublic static int lastIndexOf(char[] array, char target) Returns the index of the last appearance of the valuetargetinarray.- Parameters:
- array- an array of- charvalues, possibly empty
- target- a primitive- charvalue
- Returns:
- the greatest index ifor whicharray[i] == target, or-1if no such index exists.
 
 - 
minpublic static char min(char... array) Returns the least value present inarray.- Parameters:
- array- a nonempty array of- charvalues
- Returns:
- the value present in arraythat is less than or equal to every other value in the array
- Throws:
- IllegalArgumentException- if- arrayis empty
 
 - 
maxpublic static char max(char... array) Returns the greatest value present inarray.- Parameters:
- array- a nonempty array of- charvalues
- Returns:
- the value present in arraythat is greater than or equal to every other value in the array
- Throws:
- IllegalArgumentException- if- arrayis empty
 
 - 
constrainToRange@Beta public static char constrainToRange(char value, char min, char max) Returns the value nearest tovaluewhich is within the closed range[min..max].If valueis within the range[min..max],valueis returned unchanged. Ifvalueis less thanmin,minis returned, and ifvalueis greater thanmax,maxis returned.- Parameters:
- value- the- charvalue to constrain
- min- the lower bound (inclusive) of the range to constrain- valueto
- max- the upper bound (inclusive) of the range to constrain- valueto
- Throws:
- IllegalArgumentException- if- min > max
- Since:
- 21.0
 
 - 
concatpublic static char[] concat(char[]... arrays) Returns the values from each provided array combined into a single array. For example,concat(new char[] {a, b}, new char[] {}, new char[] {c}returns the array{a, b, c}.- Parameters:
- arrays- zero or more- chararrays
- Returns:
- a single array containing all the values from the source arrays, in order
 
 - 
toByteArray@GwtIncompatible public static byte[] toByteArray(char value) Returns a big-endian representation ofvaluein a 2-element byte array; equivalent toByteBuffer.allocate(2).putChar(value).array(). For example, the input value'\\u5432'would yield the byte array{0x54, 0x32}.If you need to convert and concatenate several values (possibly even of different types), use a shared ByteBufferinstance, or useByteStreams.newDataOutput()to get a growable buffer.
 - 
fromByteArray@GwtIncompatible public static char fromByteArray(byte[] bytes) Returns thecharvalue whose big-endian representation is stored in the first 2 bytes ofbytes; equivalent toByteBuffer.wrap(bytes).getChar(). For example, the input byte array{0x54, 0x32}would yield thecharvalue'\\u5432'.Arguably, it's preferable to use ByteBuffer; that library exposes much more flexibility at little cost in readability.- Throws:
- IllegalArgumentException- if- byteshas fewer than 2 elements
 
 - 
fromBytes@GwtIncompatible public static char fromBytes(byte b1, byte b2) Returns thecharvalue whose byte representation is the given 2 bytes, in big-endian order; equivalent toChars.fromByteArray(new byte[] {b1, b2}).- Since:
- 7.0
 
 - 
ensureCapacitypublic static char[] ensureCapacity(char[] array, int minLength, int padding) Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length. Ifarrayalready has a length of at leastminLength, it is returned directly. Otherwise, a new array of sizeminLength + paddingis returned, containing the values ofarray, and zeroes in the remaining places.- Parameters:
- array- the source array
- minLength- the minimum length the returned array must guarantee
- padding- an extra amount to "grow" the array by if growth is necessary
- Returns:
- an array containing the values of array, with guaranteed minimum lengthminLength
- Throws:
- IllegalArgumentException- if- minLengthor- paddingis negative
 
 - 
joinpublic static String join(String separator, char... array) Returns a string containing the suppliedcharvalues separated byseparator. For example,join("-", '1', '2', '3')returns the string"1-2-3".- Parameters:
- separator- the text that should appear between consecutive values in the resulting string (but not at the start or end)
- array- an array of- charvalues, possibly empty
 
 - 
lexicographicalComparatorpublic static Comparator<char[]> lexicographicalComparator() Returns a comparator that compares twochararrays lexicographically; not advisable for sorting user-visible strings as the ordering may not match the conventions of the user's locale. That is, it compares, usingcompare(char, char)), the first pair of values that follow any common prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For example,[] < ['a'] < ['a', 'b'] < ['b'].The returned comparator is inconsistent with Object.equals(Object)(since arrays support only identity equality), but it is consistent withArrays.equals(char[], char[]).- Since:
- 2.0
 
 - 
toArraypublic static char[] toArray(Collection<Character> collection) Copies a collection ofCharacterinstances into a new array of primitivecharvalues.Elements are copied from the argument collection as if by collection.toArray(). Calling this method is as thread-safe as calling that method.- Parameters:
- collection- a collection of- Characterobjects
- Returns:
- an array containing the same values as collection, in the same order, converted to primitives
- Throws:
- NullPointerException- if- collectionor any of its elements is null
 
 - 
sortDescendingpublic static void sortDescending(char[] array) Sorts the elements ofarrayin descending order.- Since:
- 23.1
 
 - 
sortDescendingpublic static void sortDescending(char[] array, int fromIndex, int toIndex) Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order.- Since:
- 23.1
 
 - 
reversepublic static void reverse(char[] array) Reverses the elements ofarray. This is equivalent toCollections.reverse(Chars.asList(array)), but is likely to be more efficient.- Since:
- 23.1
 
 - 
reversepublic static void reverse(char[] array, int fromIndex, int toIndex) Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.reverse(Chars.asList(array).subList(fromIndex, toIndex)), but is likely to be more efficient.- Throws:
- IndexOutOfBoundsException- if- fromIndex < 0,- toIndex > array.length, or- toIndex > fromIndex
- Since:
- 23.1
 
 - 
asListpublic static List<Character> asList(char... backingArray) Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[]). The list supportsList.set(int, Object), but any attempt to set a value tonullwill result in aNullPointerException.The returned list maintains the values, but not the identities, of Characterobjects written to or read from it. For example, whetherlist.get(0) == list.get(0)is true for the returned list is unspecified.- Parameters:
- backingArray- the array to back the list
- Returns:
- a list view of the array
 
 
- 
 
-