Add charset example
This commit is contained in:
parent
19e2fca9cb
commit
c6543d9662
|
@ -6,6 +6,8 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -15,6 +17,7 @@ import org.junit.Test;
|
|||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
|
@ -196,4 +199,20 @@ public class GuavaStringTest {
|
|||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRemoveCharsNotInCharset_thenRemoved() {
|
||||
final Charset charset = Charset.forName("cp437");
|
||||
final CharsetEncoder encoder = charset.newEncoder();
|
||||
|
||||
final Predicate<Character> inRange = new Predicate<Character>() {
|
||||
@Override
|
||||
public boolean apply(final Character c) {
|
||||
return encoder.canEncode(c);
|
||||
}
|
||||
};
|
||||
|
||||
final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
|
||||
assertEquals("hello", result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue