I need to be able to take a string in Java and determine whether or not all of the characters contained within it are in a specified character set (e.g. ISO-8859-1). I’ve looked around quite a bit for a simple way to do this (including playing around with a CharsetDecoder), but have yet to be able to find something.
What is the best way to take a string and determine if all the characters are within a given character set?
Class CharsetEncoder in package java.nio.charset offer a method canEncode to test if a specific character is supported.
Michael basically did something like this:
Charset.forName(CharEncoding.ISO_8859_1).newEncoder().canEncode(“string”)Note that
CharEncoding.ISO_8859_1rely on Apache commons and may be replaced by “ISO_8859_1”.