Java ASCII from toASCII(String s)

Here you can find the source of toASCII(String s)

Description

Converts a non ASCII string to an ASCII string.

License

Open Source License

Parameter

Parameter Description
s - the string which characters are going to be converted

Return

a string which all its characters are ASCII

Declaration

public static String toASCII(String s) 

Method Source Code

//package com.java2s;
/**/* w w w.  ja v a 2s .  c  om*/
 * Copyright 2016 
 * Ivan Cantador
 * Information Retrieval Group at Universidad Autonoma de Madrid
 *
 * This is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with 
 * the current software. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private static final String PLAIN_ASCII = "AaEeIiOoUu" // grave
            + "AaEeIiOoUuYy" // acute
            + "AaEeIiOoUuYy" // circumflex
            + "AaEeIiOoUuYy" // tilde
            + "AaEeIiOoUuYy" // umlaut
            + "Aa" // ring
            + "Cc" // cedilla
    ;
    private static final String UNI_CODE = "\u00C0\u00E0\u00C8\u00E8\u00CC\u00EC\u00D2\u00F2\u00D9\u00F9" // grave
            + "\u00C1\u00E1\u00C9\u00E9\u00CD\u00ED\u00D3\u00F3\u00DA\u00FA\u00DD\u00FD" // acute
            + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177" // circumflex
            + "\u00C2\u00E2\u00CA\u00EA\u00CE\u00EE\u00D4\u00F4\u00DB\u00FB\u0176\u0177" // tilde
            + "\u00C4\u00E4\u00CB\u00EB\u00CF\u00EF\u00D6\u00F6\u00DC\u00FC\u0178\u00FF" // umlaut
            + "\u00C5\u00E5" // ring
            + "\u00C7\u00E7" // cedilla
    ;

    /**
     * Converts a non ASCII string to an ASCII string.
     *
     * @param s - the string which characters are going to be converted
     *
     * @return a string which all its characters are ASCII
     */
    public static String toASCII(String s) {
        StringBuffer sb = new StringBuffer();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            int pos = UNI_CODE.indexOf(c);
            if (pos > -1) {
                sb.append(PLAIN_ASCII.charAt(pos));
            } else {
                if ((int) c < 256) {
                    sb.append(c);
                } else {
                    sb.append("?");
                }
            }
        }
        return sb.toString();
    }
}

Related

  1. toAscii(String hexStr)
  2. toAscii(String notAscii)
  3. toAscii(String s)
  4. toAscii(String s)
  5. toAscii(String s)
  6. toASCII(String str)
  7. toAsciiArray(char[] carr)
  8. toAsciiByteArray(final char[] carr)
  9. toAsciiByteArray(String s)