Skip to content

Commit c6543d9

Browse files
committed
Add charset example
1 parent 19e2fca commit c6543d9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

guava/src/test/java/org/baeldung/guava/GuavaStringTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import static org.junit.Assert.assertThat;
77
import static org.junit.Assert.assertTrue;
88

9+
import java.nio.charset.Charset;
10+
import java.nio.charset.CharsetEncoder;
911
import java.util.ArrayList;
1012
import java.util.List;
1113
import java.util.Map;
@@ -15,6 +17,7 @@
1517
import com.google.common.base.CharMatcher;
1618
import com.google.common.base.Function;
1719
import com.google.common.base.Joiner;
20+
import com.google.common.base.Predicate;
1821
import com.google.common.base.Splitter;
1922
import com.google.common.collect.Iterables;
2023
import com.google.common.collect.Lists;
@@ -196,4 +199,20 @@ public void whenCountCharInString_thenCorrect() {
196199
assertEquals(2, result);
197200
}
198201

202+
@Test
203+
public void whenRemoveCharsNotInCharset_thenRemoved() {
204+
final Charset charset = Charset.forName("cp437");
205+
final CharsetEncoder encoder = charset.newEncoder();
206+
207+
final Predicate<Character> inRange = new Predicate<Character>() {
208+
@Override
209+
public boolean apply(final Character c) {
210+
return encoder.canEncode(c);
211+
}
212+
};
213+
214+
final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
215+
assertEquals("hello", result);
216+
}
217+
199218
}

0 commit comments

Comments
 (0)