aboseraj aboseraj - 5 months ago 16
Android Question

replaceAll all characters in statement to new characters

i have EditText the user can write and statement

i need to replaceAll all characters to new characters .

this arrayOfChar1 char array for ASCII code for all characters from a to z
i need to change all characters user input in edittext to all new characters from this array arrayOfString1
but i don't know how to replaceAll all characters

Example for input from user: "how are you"
the output : "нσω αяє yσυ"

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str2 = edit.getText().toString();
char[] arrayOfChar1 = { 97, 98, 99, 100, 101};
String[] arrayOfString1 = { "α", "в", "c",};

}
});

Answer

A HashMap would be better suited for your task.

final static HashMap<Character, String> replacements = new HashMap<Character, String>() {{
    put('a', "α" );
    put('b', "в"); 
    put('c', "c");
    put('d', "ɒ");
    put('e', "є");
    // ...
}};

Code to translate

String str = "abbacde";
char[] chars = str.toCharArray();
for (char c : chars) {
    String replacement = replacements.get(c);
    str = str.replaceAll(""+c, replacement);
}

System.out.println(str); // αввαcɒє
Comments