Skip to content

Instantly share code, notes, and snippets.

@alefq
Created April 10, 2015 01:55
Show Gist options
  • Save alefq/e3407d0b3c3cf9a4af4f to your computer and use it in GitHub Desktop.
Save alefq/e3407d0b3c3cf9a4af4f to your computer and use it in GitHub Desktop.
package py.com.sodep.utilities;
import java.util.HashMap;
import java.util.Map;
/**
* Converts Spanish letters to its equivalent in US ASCII
*
*/
public class StringUtils {
private static Map<Character, Character> spanish2ASCII = new HashMap<Character, Character>();
static {
char[] spanish = new char[] { 'á', 'é', 'í', 'ó', 'ú', 'ñ', 'Á', 'É',
'Í', 'Ó', 'Ú', 'Ñ' };
char[] us = new char[] { 'a', 'e', 'i', 'o', 'u', 'n', 'A', 'E', 'I',
'O', 'U', 'N' };
for (int i = 0; i < spanish.length; i++) {
spanish2ASCII.put(spanish[i], us[i]);
}
}
private StringUtils() {
}
public static void main(String[] args) {
String extended = "áéíóúñÁÉÍÓÚÑ";
System.out.println(extended + ": " + StringUtils.toACII7bit(extended));
}
public static String toACII7bit(String data) {
StringBuilder ret = new StringBuilder();
System.out.println(spanish2ASCII);
if (data != null) {
for (int i = 0; i < data.length(); i++) {
Character currentChar = data.charAt(i);
if (spanish2ASCII.containsKey(currentChar)) {
ret.append(spanish2ASCII.get(currentChar));
} else {
ret.append(currentChar);
}
}
}
return ret.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment