java 如何从一个文本规范化任何字符串?[已关闭]

5us2dqdw  于 2022-12-28  发布在  Java
关注(0)|答案(1)|浏览(104)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
我有这个在Txt与'ANSI' unicode

VILLAFAÝE GABRIELA LILIANA

我想得到这个

VILLAFAYE GABRIELA LILIANA

有时我有these situations(正确的拼写是“Roque Saenz Peña”)
这是我的java代码

public String isNormalizada(String cadena) {
         if (! Normalizer.isNormalized(cadena, Normalizer.Form.NFD)){
             cadena = Normalizer.normalize(cadena, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
          }
         return cadena;
    }

这是我如何打开文件

BufferedWriter  br = null;  
    FileWriter swResultados =  null;
    String line;
    String line2;
    int contador=0;

    
    public void LeerBase() {
        BufferedReader file = null;
        
        try {

            file = new BufferedReader (new FileReader(PathOrigen));
            line = file.readLine();
        }catch(Exception e) {System.out.println("Error de lectura del fichero");}
        
        try{
             swResultados = new FileWriter(PathDestino,true); 
             br = new BufferedWriter(swResultados);

          } catch (Exception e){}
    }

如何正确地规范化字符串,去掉““或"“?

uqcuzwp8

uqcuzwp81#

我不知道您在哪里调用isNormalizada方法?
我将这样实现:

public String isNormalizada(String cadena) {
    if (!Normalizer.isNormalized(cadena, Normalizer.Form.NFD)) {
        cadena = Normalizer.normalize(cadena, Normalizer.Form.NFD);
        cadena = cadena.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    }
    return cadena;
}

然后当你读你的档案时:

String line = file.readLine();
line = isNormalizada(line);

最后,在文件中写入规范化字符串:

br.write(line);

所以一切都在一起:

BufferedWriter  br = null;  
    
    public void LeerBase() {
        BufferedReader file = null;
        
        try {

            file = new BufferedReader (new FileReader(PathOrigen));
            String line = file.readLine();
            line = isNormalizada(line);
        }catch(Exception e) {System.out.println("Error de lectura del fichero");}
        
        try{
             br.write(line);

          } catch (Exception e){}
    }

相关问题