Java ASCII from toAscii(String s)

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

Description

Replaces accented characters from a non-null String by their ascii equivalent.

License

Open Source License

Declaration

public static String toAscii(String s) 

Method Source Code

//package com.java2s;
/*/*from   w w w  . ja v a  2  s  .co  m*/
 * Copyright (c) 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id: StringUtils.java 28482 2008-01-04 15:33:39Z sfermigier $
 */

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

    /**
     * Replaces accented characters from a non-null String by their ascii
     * equivalent.
     */
    public static String toAscii(String s) {
        StringBuilder sb = new StringBuilder();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);
            int pos = UNICODE.indexOf(c);
            if (pos > -1) {
                sb.append(PLAIN_ASCII.charAt(pos));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

Related

  1. toAscii(int i, boolean reversed)
  2. toAscii(int number)
  3. toAscii(String hexStr)
  4. toAscii(String notAscii)
  5. toAscii(String s)
  6. toASCII(String s)
  7. toAscii(String s)
  8. toASCII(String str)
  9. toAsciiArray(char[] carr)