/* * [TestNormalizer.java] * * Summary: Exercise Normalizer class, JDK 1.8+. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2009-01-01 initial version */ package com.mindprod.example; import java.text.Normalizer; import static java.lang.System.*; /** * Exercise Normalizer class, JDK 1.8+. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2009-01-01 initial version * @since 2009-01-01 */ public final class TestNormalizer { /** * Convert accented letters to the equivalent unaccented letter. * * @param accented string to convert * * @return string with accented letters converted to equivalent unaccented letters */ private static String removeAccents( String accented ) { // convert accented chars to equivalent unaccented char + dead char accent pair. // See http://www.unicode.org/unicode/reports/tr15/tr15-23.html no understand the NFD transform. final String normalized = Normalizer.normalize( accented, Normalizer.Form.NFD ); // remove the dead char accents, leaving just the unaccented chars. // Stripped string should have the same length as the original accented String. StringBuilder sb = new StringBuilder( accented.length() ); for ( int i = 0; i < normalized.length(); i++ ) { char c = normalized.charAt( i ); if ( Character.getType( c ) != Character.NON_SPACING_MARK ) { sb.append( c ); } } return sb.toString(); } /** * Test harness * * @param args not used */ public static void main( String[] args ) { String godel = "Kurt Gödel"; out.println( "godel = " + godel ); out.println( "removeAccents( godel ) = " + removeAccents( godel ) ); } }