0

I need to replace multiple characters with multiple replacements in a String. I want to replace the following letters:

Z, I, K, A, Y, W, X, F, S, Q, V, G, N, O, H, J, B, P, T, R, M, E, L, C, U, D

with the corresponding:

E, T, O, A, I, N, R, S, H, L, D, F, M, C, U, G, W, Y, B, P, V, K, J, Q, Z, X

For example, 'Z' becomes 'E', 'I' becomes 'T', etc. Here's some example code:

public static void main(String[] args) {
    String s = "LAMA XZRQAOZ";
    // Replacing code
    // Output: s = "JAVA REPLACE"
}

What is the simplest way to do this?

Note: I have tried using an array along with a for loop, but that replaces the letter more than once, so it replaces it into the wrong letter.

Note2: The String I'm actually trying to do is over 10000 characters long, so I don't know if iterating 1 character at a time will be too slow in performance.

3
  • Use HashMap. Commented Mar 22, 2015 at 2:50
  • Aside: this looks like a simple substitution cipher. You should be aware that such ciphers are simple to crack. I could do it in 20 minutes with a pencil and paper :-) Commented Mar 22, 2015 at 3:10
  • @StephenC Yeah, I'm trying to crack substitution cipher text. I know how to do it by hand, but I wanted to write a program for it. Commented Mar 22, 2015 at 3:29

1 Answer 1

2

The simplest way is to load the String into a StringBuilder and then iterate through the StringBuilder, testing and replacing the characters one at a time. Pick the replacement characters either in a switch statement, or by using a Map lookup.

(A Map lookup is simpler / less code. A switch statement should be faster.)

See @Daniel Nugent's answer for an example solution ...


I suspect that your failed attempt was like this:

  for (replacement : ...) {
     str = str.replaceAll(...);
  }

Obviously, that doesn't work because the replaced characters are then potentially subjected to the next round of replacement.

In fact, it is not practical to implement this form of replacement using any of the existing String.replace... methods.


Note2: The String I'm actually trying to do is over 10000 characters long, so I don't know if iterating 1 character at a time will be too slow in performance.

It won't be. In fact it will be orders of magnitude faster than multiple calls to replace, because the replace calls also have to iterate over the string one character at a time behind the scenes. Indeed, it is impossible to do replacements of individual characters in a String without stepping through each character position.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed on all counts, good answer. Calling replace() or replaceAll() for each character would not be a good solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.