Issue
I need to replace multiple literals by digits in a string like "FF432423FA112". "A"=0, "B"=1 etc. I've tried to do it in a loop like but it didn't work. Tried also with char array
String test = "FF432423FA112";
String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
for (int i=0; i<letters.length; i++) {
newStr = test.replace(letters[i],i);
}
Solution
That is because you forget to use newStr.replace
, every time loop will generate the last test.replace
copy character
String test = "FF432423FA112";
String[] letters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
String newStr = test;
for (int i=0; i<letters.length; i++) {
newStr = newStr.replace(letters[i], i + "");
}
System.out.println(newStr);
Answered By - 某某某
Answer Checked By - Katrina (JavaFixing Volunteer)