By using
CharMatcher class, we can represent particular class of characters like digits,
white spaces, invisible characters etc.,
How to obtain CharMatcher instance
1.
Using
constants
2.
Using
methods provided by CharMatcher class
Obtain CharMatcher instance using constants
CharMatcher
class provides following constants to obtain CharMatcher instance.
Constant
|
Description
|
ANY
|
Matches
any character.
|
ASCII
|
Matches
any ASCII character
|
BREAKING_WHITESPACE
|
Matches a
whitespace which can be interpreted as a break between words for formatting
purposes
|
DIGIT
|
Matches a
digit. It matches digits specified by Unicode.
|
INVISIBLE
|
Match
invisible characters like SPACE_SEPARATOR, LINE_SEPARATOR,
PARAGRAPH_SEPARATOR.
|
JAVA_DIGIT
|
Match a
digit as per isDigit function defined in java.lana.Character class.
|
JAVA_ISO_CONTROL
|
Matches an
ISO control character as specified by isISOControl method of
java.lang.Character class.
|
JAVA_LETTER
|
Matches a
character according to isLetter method of java.lang.Character class.
|
JAVA_LETTER_OR_DIGIT
|
Matches a
character whether a character is a letter or digit according to
sLetterOrDigit method of java.lang.Character class.
|
JAVA_LOWER_CASE
|
Matches a
lower case character according to isLowerCase method of Character class.
|
JAVA_UPPER_CASE
|
Matches a upper
case character according to isUpperCase method of Character class.
|
NONE
|
Matches no
characters
|
SINGLE_WIDTH
|
Determines
whether a character is single-width.
|
WHITESPACE
|
Determines
whether a character is whitespace according to the latest Unicode standard.
|
Using methods provided by CharMatcher class
You can get
instance of CharMatcher class by using following methods.
Method
|
Description
|
anyOf(final
CharSequence sequence)
|
Returns a
char matcher that matches any character present in the given character
sequence.
|
inRange(final
char startInclusive, final char endInclusive)
|
Returns a
char matcher that matches any character in a given range (Inclusive).
|
is(final
char match)
|
Returns a
char matcher that matches only one specified character.
|
isNot(final
char match)
|
Returns a
char matcher that matches any character except the one specified.
|
noneOf(CharSequence
sequence)
|
Returns a
char matcher that matches any character not present in the given character
sequence.
|
1. Trim characters from beginning and end
of string
Following
methods are used to trim given characters from beginning and end of the string.
Method
|
Description
|
String
trimFrom(CharSequence sequence)
|
Omits all
characters that this matcher matches from the beginning and from the end of
the string.
|
String
trimLeadingFrom(CharSequence sequence)
|
Omits all
characters that this matcher matches from the beginning and from the end of
the string.
|
String
trimTrailingFrom(CharSequence sequence)
|
Omits all
characters that this matcher matches from the end of the string.
|
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.DIGIT; String sequence = "1234Hello hari123 How are you12233"; String result1 = matcher.trimFrom(sequence); String result2 = matcher.trimLeadingFrom(sequence); String result3 = matcher.trimTrailingFrom(sequence); System.out.println("result1 = " + result1); System.out.println("result2 = " + result2); System.out.println("result3 = " + result3); } }
Output
result1 = Hello hari123 How are you result2 = Hello hari123 How are you12233 result3 = 1234Hello hari123 How are you
2. Remove multiple tabs and consecutive
spaces
By using
following methods you can remove multiple tabs and spaces.
Method
|
Description
|
String
collapseFrom(CharSequence sequence, char replacement)
|
Replace
each group of matching characters with given character.
Ex: CharMatcher.anyOf("abd").collapseFrom("bbrakadabra",
'_'); statement returns “_r_k_r_”
|
String
trimAndCollapseFrom(CharSequence sequence, char replacement)
|
Replace
each group of matching characters with given character, except that groups of
matching characters at the start or end of the sequence are removed without
replacement.
Ex:
CharMatcher.anyOf("abd").trimAndCollapseFrom("bbrakadabra",
'_'); statement returns “r_k_r”.
|
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.WHITESPACE; String sequence = " Hello How Are You "; String result1 = matcher.collapseFrom(sequence, ' '); String result2 = matcher.trimAndCollapseFrom(sequence, ' '); System.out.println("result1 = " + result1); System.out.println("result2 = " + result2); } }
Output
result1 = Hello How Are You result2 = Hello How Are You
3. Get the number of matching characters
“countIn”
method is used to get number of matching characters in given sequence.
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.anyOf("aeiou"); String sequence = "Hello, How are you"; int vowels = matcher.countIn(sequence); System.out.println("Number of vowels in '" + sequence + "'are " + vowels); } }
Output
Number of vowels in 'Hello, How are you'are 7
4. Get index of matching characters
Following methods are used to get
the index of matching character.
Method
|
Description
|
int indexIn(CharSequence
sequence)
|
Returns the index of first
matching character in given input sequence.
|
int indexIn(CharSequence
sequence, int start)
|
Returns the index of first
matching character in given input sequence, from given starting index.
|
int lastIndexIn(CharSequence
sequence)
|
Returns the index of the last
matching character in a character sequence
|
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.anyOf("aeiou"); String sequence = "Hello, How are you"; int firstIndex = matcher.indexIn(sequence); int nextIndex = matcher.indexIn(sequence, firstIndex + 1); int lastIndex = matcher.lastIndexIn(sequence); System.out.println("First Index is " + firstIndex); System.out.println("Next Index is " + nextIndex); System.out.println("Last Index is " + lastIndex); } }
Output
First Index is 1 Next Index is 4 Last Index is 17
5. Matches given characters
Method
|
Description
|
boolean
matches(char c)
|
Returns
true or false value for the given character.
|
boolean
matchesAllOf(CharSequence sequence)
|
Returns
true if a character sequence contains only matching characters.
|
boolean
matchesAnyOf(CharSequence sequence)
|
Returns
true if a character sequence contains at least one matching character.
|
boolean
matchesNoneOf(CharSequence sequence)
|
Returns
true if a character sequence contains no matching characters.
|
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.anyOf("aeiou"); System.out.println("Is matcher matches 'e'" + matcher.matches('e')); System.out.println("Is matcher matches all characters 'lru' " + matcher.matchesAllOf("lru")); System.out.println("Is matcher matches any of 'uvwxyz'" + matcher.matchesAnyOf("uvwxyz")); System.out.println("Is matcher don't match any of 'vwxyz'" + matcher.matchesNoneOf("vwxyz")); } }
Output
Is matcher matches 'e'true Is matcher matches all characters 'lru' false Is matcher matches any of 'uvwxyz'true Is matcher don't match any of 'vwxyz'true
6. Create negative matcher
CharMatcher class provides negate
method, which returns a matcher that matches any character not matched by this
matcher.
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.DIGIT; int index = matcher.indexIn("12345Hello"); System.out.println("Index of first digit is " + index); matcher = matcher.negate(); index = matcher.indexIn("12345Hello"); System.out.println("Index of first character is " + index); } }
Output
Index of first digit is 0 Index of first character is 5
7. Remove matching characters from given
string
CharMatcher
class provides removeFrom method, which removes all matching characters from
given string.
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.DIGIT; String sequence = "12345Hello How1287 are 9886you8"; String result = matcher.removeFrom(sequence); System.out.println(result); } }
Output
Hello How are you
8.Replace matching characters with given
string
CharMatcher
class provides two forms of replace methods, which replace matching character
with some other string/character.
String replaceFrom(CharSequence sequence,
CharSequence replacement)
String replaceFrom(CharSequence sequence, char replacement)
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.anyOf("a"); String sequence = "abrakadabra"; String result = matcher.replaceFrom(sequence, "aaa"); System.out.println(result); } }
Output
aaabraaakaaadaaabraaa
9.Retaing only matching characters
CharMatcher class provides retainFrom
method, to retain only matching characters.
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.anyOf("ab"); String sequence = "abrakadabra"; String result = matcher.retainFrom(sequence); System.out.println(result); } }
Output
abaaaba
10. Logically connect matcher
CharMatcher class provides and, or
methods to logically connect more than one matcher.
Method
|
Description
|
CharMatcher and(CharMatcher other)
|
Returns a matcher that matches any
character matched by both this matcher and other.
|
CharMatcher or(CharMatcher other)
|
Returns a matcher that matches any
character matched by either this matcher or other.
|
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher digitMatcher = CharMatcher.anyOf("1234567890ae"); CharMatcher vowelsMatcher = CharMatcher.anyOf("aeiou"); CharMatcher andMatcher = digitMatcher.and(vowelsMatcher); CharMatcher orMatcher = digitMatcher.or(vowelsMatcher); String result1 = andMatcher.removeFrom("12345hfahfashas0909"); String result2 = orMatcher.removeFrom("12345hfahfashas0909"); System.out.println(result1); System.out.println(result2); } }
Output
12345hfhfshs0909
hfhfshs
11. Match character in given range
CharMatcher class provides inRange
method, to match characters in given range.
Following program removes all lower
case letters.
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher alphaMatcher = CharMatcher.inRange('a', 'z'); String sequence = "Hello123abceZebra"; String result = alphaMatcher.removeFrom(sequence); System.out.println(result); } }
Output
H123Z
12. Match single character
By using is method of CharMatcher
class, you can match exactly one character.
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.is('p'); String sequence = "ptrptrptrptrptr"; System.out.println(matcher.removeFrom(sequence)); System.out.println(matcher.retainFrom(sequence)); } }
Output
trtrtrtrtr ppppp
13. Match all characters other than one
character
CharMatcher class provides isNot method
to match all characters, other than given character. It is revers of ‘is’
method.
import com.google.common.base.CharMatcher; public class CharMatcherEx { public static void main(String args[]) { CharMatcher matcher = CharMatcher.isNot('p'); String sequence = "ptrptrptrptrptr"; System.out.println(matcher.removeFrom(sequence)); System.out.println(matcher.retainFrom(sequence)); } }
Output
ppppp trtrtrtrtr
No comments:
Post a Comment