In this quick tutorial we’ll explore the CharMatcher utility class in Guava.
在这个快速教程中,我们将探索Guava中的CharMatcher实用类。
1. Remove Special Characters from a String
1.从一个字符串中删除特殊字符
Let’s start by removing all special characters from a String.
让我们先从字符串中删除所有特殊字符。
In the following example, we remove all characters that aren’t digit or letter using retainFrom():
在下面的例子中,我们使用retainFrom()删除所有非数字或字母的字符。
@Test
public void whenRemoveSpecialCharacters_thenRemoved(){
String input = "H*el.lo,}12";
CharMatcher matcher = CharMatcher.javaLetterOrDigit();
String result = matcher.retainFrom(input);
assertEquals("Hello12", result);
}
2. Remove Non ASCII Characters From String
2.删除字符串中的非ASCII字符
We can also use CharMatcher to remove non ASCII characters from a String as in the following example:
我们还可以使用CharMatcher从String中删除非ASCII字符,如下例所示。
@Test
public void whenRemoveNonASCIIChars_thenRemoved() {
String input = "あhello₤";
String result = CharMatcher.ascii().retainFrom(input);
assertEquals("hello", result);
result = CharMatcher.inRange('0', 'z').retainFrom(input);
assertEquals("hello", result);
}
3. Remove Characters Not in the Charset
3.删除不在字符集的字符
Now – let’s see how to remove Characters that don’t belong to a given Charset. In the following example – we’ll remove characters that don’t belong to the “cp437” Charset:
现在–让我们看看如何删除不属于某个字符集的字符。在下面的例子中–我们将删除不属于 “cp437” Charset的字符。
@Test
public void whenRemoveCharsNotInCharset_thenRemoved() {
Charset charset = Charset.forName("cp437");
CharsetEncoder encoder = charset.newEncoder();
Predicate<Character> inRange = new Predicate<Character>() {
@Override
public boolean apply(Character c) {
return encoder.canEncode(c);
}
};
String result = CharMatcher.forPredicate(inRange)
.retainFrom("helloは");
assertEquals("hello", result);
}
Note: We used CharsetEncoder to create a Predicate that check if the given Character can be encoded to the given Charset.
注意:我们使用CharsetEncoder来创建一个Predicate,检查给定的Character是否可以被编码到给定的Charset。
4. Validate String
4.验证字符串
Next – let’s see how to validate a String using CharMatcher.
接下来–让我们看看如何使用CharMatcher验证一个String。
We can use matchesAllOf() to check if all characters match a condition. And we can make use of matchesNoneOf() to check if a condition doesn’t apply on any of the String characters.
我们可以使用matchesAllOf()来检查所有字符是否符合条件。我们还可以使用matchesNoneOf()来检查条件是否不适用于任何String字符。
In the following example, we check if our String is lowercase, contains at least one ‘e‘ character and doesn’t contain any digits:
在下面的例子中,我们检查我们的String是否是小写的,至少包含一个’e‘字符,并且不包含任何数字。
@Test
public void whenValidateString_thenValid(){
String input = "hello";
boolean result = CharMatcher.javaLowerCase().matchesAllOf(input);
assertTrue(result);
result = CharMatcher.is('e').matchesAnyOf(input);
assertTrue(result);
result = CharMatcher.javaDigit().matchesNoneOf(input);
assertTrue(result);
}
5. Trim String
5.修剪字符串
Now – let’s see how trim a String using CharMatcher.
现在–让我们看看如何使用CharMatcher修剪一个字符串。
In the following example, we use trimLeading(), trimTrailing and trimFrom() to trim our String:
在下面的例子中,我们使用trimLeading(), trimTrailing 和 trimFrom() 来修剪我们的String。
@Test
public void whenTrimString_thenTrimmed() {
String input = "---hello,,,";
String result = CharMatcher.is('-').trimLeadingFrom(input);
assertEquals("hello,,,", result);
result = CharMatcher.is(',').trimTrailingFrom(input);
assertEquals("---hello", result);
result = CharMatcher.anyOf("-,").trimFrom(input);
assertEquals("hello", result);
}
6. Collapse a String
6.折叠一个字符串
Next – let’s see how to collapse a String using CharMatcher.
接下来–让我们看看如何使用CharMatcher来折叠一个String。
In the following example, we use collapseFrom() to replace consecutive spaces with ‘–‘:
在下面的例子中,我们使用collapseFrom()来用’–‘替换连续的空格。
@Test
public void whenCollapseFromString_thenCollapsed() {
String input = " hel lo ";
String result = CharMatcher.is(' ').collapseFrom(input, '-');
assertEquals("-hel-lo-", result);
result = CharMatcher.is(' ').trimAndCollapseFrom(input, '-');
assertEquals("hel-lo", result);
}
7. Replace from String
7.从字符串替换
We can use CharMatcher to replace specific characters from a String as in the following example:
我们可以使用CharMatcher来替换String中的特定字符,如下例所示。
@Test
public void whenReplaceFromString_thenReplaced() {
String input = "apple-banana.";
String result = CharMatcher.anyOf("-.").replaceFrom(input, '!');
assertEquals("apple!banana!", result);
result = CharMatcher.is('-').replaceFrom(input, " and ");
assertEquals("apple and banana.", result);
}
8. Count Character Occurrences
8.计算字符出现的次数
Finally – let’s see how to count the occurrences of characters using CharMatcher.
最后–让我们看看如何使用CharMatcher计算字符的出现次数。
In the following example, we count the commas and characters between ‘a‘:’h‘:
在下面的例子中,我们计算’a‘:’h‘之间的逗号和字符。
@Test
public void whenCountCharInString_thenCorrect() {
String input = "a, c, z, 1, 2";
int result = CharMatcher.is(',').countIn(input);
assertEquals(4, result);
result = CharMatcher.inRange('a', 'h').countIn(input);
assertEquals(2, result);
}
9. Conclusion
9.结论
In this article we illustrated some of the more useful APIs and real-world usage examples of using Guava for Strings.
在这篇文章中,我们说明了一些比较有用的API和使用Guava的字符串的真实使用例子。
The full source code is available over on GitHub.
完整的源代码可在GitHub上获得,。