How to dynamically remove all the treble accents contained in a String?

Asked

Viewed 90 times

1

How to dynamically remove all treble accents contained in a String?

Example: "solid synthetic sofa".

void funcao(String texto) {
  var resultado = '';
  for (var i = texto.length - 1; i >= 0; i--) {
    resultado += texto[i];
  }
  print(resultado);
}

void main() {
  funcao('Olá mundo');
}

2 answers

0

The normalize() method returns the Unicode Normalization Form (Unicode Normalization Form) of a given string. Normalize method()

var string = "sofá sintético sólido";
var string_norm = string.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
console.log(string_norm);


var string2 = "O método normalize() retorna a Forma de Normalização Unicode (Unicode Normalization Form) de uma dada string";
var string_norm2 = string2.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
console.log(string_norm2);

0


You can do it this way:

void main() {
  String s = "Eaí? Como vai você?";
  print(removerAcentos(s));
}

String removerAcentos(String str) {

  var comAcento = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž';
  var semAcento = 'AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz'; 

  for (int i = 0; i < comAcento.length; i++) {      
    str = str.replaceAll(comAcento[i], semAcento[i]);
  }

  return str;
}

There may be other ways to treat accents (maybe even better), but this is one of them.

Explanation

The method remove Centos will traverse each existing character in the variable comAcento and exchange all occurrences within the given text of that character by the character at the same position as the variable hundred.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.