user2875395
user2875395

Reputation:

How to replace numbers in String with X

For Formatting account Number, i need to replace all numbers in account number with 'X'. Is there any easy way to this other than iterating over String, compare each characters if it lies between 0-9 and replace. Below is sample accountNumber.

12SRVV5E4

Upvotes: 1

Views: 351

Answers (1)

Dark Knight
Dark Knight

Reputation: 8347

You can make use of regex for this, Please refer to below code. Here \d will match integers within String and replace it with X character. Hope this answers your doubt.

String accNumber="12SRVV5E4";
accNumber= accNumber.replaceAll("\\d", "X");
System.out.println(accNumber);

Upvotes: 3

Related Questions