Remove spaces and special characters from a string

Asked

Viewed 2,083 times

0

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

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

This value is converted into String

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

After converting, take the value stringEtListName and create a table in the database, but if the user puts some special character and/or spaces, it needs to be removed, because the database does not accept tables with the same.
How I do these removals?
Observing: Remembering that I don’t want you to remove the lyrics, I want you to remove only the special character or space.
Example:

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

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

Att.
Giovani Rodrigo

1 answer

1

You can at first solve this problem by creating a method that receives a string as input and return the sentence without the accented characters. First you need to use the method normalize() to normalize their string input using Unicode normalization forms, soon after using the replaceAll() with a regular expression to replace special characters by removing no characters ASCII. Behold:

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

Exit

Lista Teste aoe

Below is the method:

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

Browser other questions tagged

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