Remove spaces and special characters from a string

0

Hello, in my project I have a EditText that receives a value entered by the user.

EditText etListName = (EditText) findViewById(R.id.etListName);

This value is converted to String

String stringEtListName = etListName.getText().toString();

After converting, I get the value stringEtListName and create a table in the database, but if the user places any special characters and / or spaces, it needs to be removed because the database does not accept tables with the same. How do I do these removals? Note: Remembering that I do not want you to remove the letter, I want you to remove only the special character or space. / p>

-- Certo
Usuário digitou: Lista Teste ãôé
Converção para: ListaTesteaoe

--Errado
Usuário digitou: Lista Teste ãôé
Converção para: ListaTeste

Att.
Giovani Rodrigo

    
asked by anonymous 15.04.2017 / 22:32

1 answer

1

You can at first solve this problem by creating a method that takes a string as input and returns the phrase without the accented characters. First you need to use the normalize() method to normalize your string using Unicode standardization forms , then use the replaceAll() with a regular expression to replace the special characters by removing characters not ASCII . See:

String value = removeAccent("Lista Teste ãôé");

Output

Lista Teste aoe

The following is the method below:

public String removeAccent(final String str) {
    String strNoAccent = Normalizer.normalize(str, Normalizer.Form.NFD);
    strNoAccent = strNoAccent.replaceAll("[^\p{ASCII}]", "");
    return strNoAccent;
}
    
15.04.2017 / 23:04