CharSet¶
- public class org.apache.commons.lang3.CharSet implements Serializable[source]¶
A set of characters.
Instances are immutable, but instances of subclasses may not be.
#ThreadSafe#
- Since:
1.0
Constructors¶
Constructs a new CharSet using the set syntax. |
Fields and constants¶
A CharSet defining ASCII alphabetic characters “a-zA-Z”. |
|
A CharSet defining ASCII alphabetic characters “a-z”. |
|
A CharSet defining ASCII alphabetic characters “A-Z”. |
|
A CharSet defining ASCII alphabetic characters “0-9”. |
|
A Map of the common cases used in the factory. |
|
A CharSet defining no characters. |
- public static final CharSet ASCII_ALPHA[source]¶
A CharSet defining ASCII alphabetic characters “a-zA-Z”.
- Since:
2.0
- public static final CharSet ASCII_ALPHA_LOWER[source]¶
A CharSet defining ASCII alphabetic characters “a-z”.
- Since:
2.0
- public static final CharSet ASCII_ALPHA_UPPER[source]¶
A CharSet defining ASCII alphabetic characters “A-Z”.
- Since:
2.0
- public static final CharSet ASCII_NUMERIC[source]¶
A CharSet defining ASCII alphabetic characters “0-9”.
- Since:
2.0
Methods¶
Add a set definition string to the |
|
Tests whether this |
|
Compares two |
|
Gets a new CharSet using the syntax described below. |
|
Gets a hash code compatible with the equals method. |
|
Gets a string representation of the set. |
- protected void add(String str)[source]¶
Add a set definition string to the
CharSet.- Parameters:
str – set definition string
- public boolean contains(char ch)[source]¶
Tests whether this
CharSetcontain the specified characterch.Examples using the negation character:
CharSet.getInstance("^a-c").contains('a') = false CharSet.getInstance("^a-c").contains('d') = true CharSet.getInstance("^^a-c").contains('a') = true // (only '^' is negated) CharSet.getInstance("^^a-c").contains('^') = false CharSet.getInstance("^a-cd-f").contains('d') = true CharSet.getInstance("a-c^").contains('^') = true CharSet.getInstance("^", "a-c").contains('^') = true
- Parameters:
ch – The character to check.
- Returns:
trueif the set contains the characters.
- @Override public boolean equals(Object obj)[source]¶
Compares two
CharSetobjects, returning true if they represent exactly the same set of characters defined in the same way.The two sets
abcanda-care not equal according to this method.- Parameters:
obj – The object to compare.
- Returns:
true if equal.
- Since:
2.0
- public static CharSet getInstance(String... setStrs)[source]¶
Gets a new CharSet using the syntax described below.
nullor empty string (“”) - set containing no charactersSingle character, such as “a” - set containing just that character
Multi character, such as “a-e” - set containing characters from one character to the other
Negated, such as “^a” or “^a-e” - set containing all characters except those defined
Combinations, such as “abe-g” - set containing all the characters from the individual sets
The matching order is:
Negated multi character range, such as “^a-e”
Ordinary multi character range, such as “a-e”
Negated single character, such as “^a”
Ordinary single character, such as “a”
Matching works left to right. Once a match is found the search starts again from the next character.
If the same range is defined twice using the same syntax, only one range will be kept. Thus, “a-ca-c” creates only one range of “a-c”.
If the start and end of a range are in the wrong order, they are reversed. Thus “a-e” is the same as “e-a”. As a result, “a-ee-a” would create only one range, as the “a-e” and “e-a” are the same.
The set of characters represented is the union of the specified ranges.
There are two ways to add a literal negation character (
^):As the last character in a string, e.g.
CharSet.getInstance("a-z^")As a separate element, e.g.
CharSet.getInstance("^", "a-z")
Examples using the negation character:
CharSet.getInstance("^a-c").contains('a') = false CharSet.getInstance("^a-c").contains('d') = true CharSet.getInstance("^^a-c").contains('a') = true // (only '^' is negated) CharSet.getInstance("^^a-c").contains('^') = false CharSet.getInstance("^a-cd-f").contains('d') = true CharSet.getInstance("a-c^").contains('^') = true CharSet.getInstance("^", "a-c").contains('^') = true
All CharSet objects returned by this method will be immutable.
- Parameters:
setStrs – Strings to merge into the set, may be null.
- Returns:
A CharSet instance.
- Since:
2.4