-3

I want to replace a character in String to numbers. For instance,

Input: abcdefghia
Output: 1234567891

Actually in instead of numbers could be any other character even in other languages

So my idea is to create a program that replaces characters into characters but in another language. I would like to know best practices how to do such tasks like this.

Here is my solution. We could use regular expression and create patterns for each character and then use method replaceall() for replacing all characters in String

SkyLexxX
  • 25
  • 7
  • Why should you use a regular expression if you are going to replace ANY character in a string? – ALFA Mar 08 '19 at 10:23

1 Answers1

1

Java > 8 has a replaceAll with a lambda:

String output = Pattern.compile("[A-z]").matcher(input)
    .replaceAll(mr -> String.valueOf((mr.group().charAt(0) % 32) % 10));

As letters start at a 32 range + 1, a -> 1, ..., i -> 9, j -> 0, k -> 1, ...

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138