Java String Accent replaceAccentedChars(StringBuilder buffer)

Here you can find the source of replaceAccentedChars(StringBuilder buffer)

Description

replace Accented Chars

License

Apache License

Declaration

public static int replaceAccentedChars(StringBuilder buffer) 

Method Source Code

//package com.java2s;
/*//from   ww w.j a v a2 s. c om
 * #%L
 * ch-commons-charset
 * %%
 * Copyright (C) 2012 Cloudhopper by Twitter
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.text.Normalizer;

public class Main {

    public static int replaceAccentedChars(StringBuilder buffer) {
        // save the size before we strip out the accents
        int sizeBefore = buffer.length();
        // each accented char will be converted into 2 chars -- the ascii version
        // followed by the accent character
        String s = Normalizer.normalize(buffer, Normalizer.Form.NFD);
        // new size will include accented chars
        int sizeAfter = s.length();
        // efficiency check #1 - if the length hasn't changed, do nothing
        int replaced = sizeAfter - sizeBefore;
        if (replaced <= 0) {
            return 0;
        }

        // replace the accents with nothing
        s = s.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
        buffer.setLength(0);
        buffer.append(s);

        return replaced;
    }
}

Related

  1. removeAccents(String text)
  2. removeAccents(String textWithAccent)
  3. removeAccentsAndNonStandardCharacters(String string)
  4. removeAccentuation(String str)
  5. replaceAccent(String strInit)
  6. replaceAccents(String string)
  7. stripAccents(final String input)
  8. stripAccents(final String input)
  9. stripAccents(final String s)