Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final Pattern NOT_ASCII = compile("[^\\p{ASCII}]")
- private static String OS = System.getProperty("os.name").toLowerCase()
- /**
- * Remove a acentuação do texto.
- * @param text Texto com acentuação.
- * @return Texto sem acentuação.
- */
- public static String removeAccents(String text) {
- if ((OS.indexOf("win") >= 0)) {
- if (!text) {
- return text
- }
- if (isValidUTF8(text.getBytes())) {
- try {
- Charset utf8charset = Charset.forName("UTF-8");
- Charset iso88591charset = Charset.forName("ISO-8859-1");
- ByteBuffer inputBuffer = ByteBuffer.wrap(text.getBytes());
- // decode ISO-8559-1
- CharBuffer data = utf8charset.decode(inputBuffer);
- // encode UTF-8
- ByteBuffer outputBuffer = iso88591charset.encode(data);
- byte[] outputData = outputBuffer.array();
- text = new String(outputData);
- } catch (Exception ignored) {
- }
- }
- String normalized = normalize(text, NFD);
- return NOT_ASCII.matcher(normalized).replaceAll("");
- }
- return text;
- }
- public static boolean isValidUTF8(byte[] input) {
- try {
- Charset.forName("UTF-8").newDecoder().decode(ByteBuffer.wrap(input));
- return true;
- } catch (CharacterCodingException ignored) {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement