Joshua
Joshua

Reputation: 29

How can I mask the first 4 digits of a stringed number

I want to mask the first 4 digits of a stringed number, for example, 1234567 would look like ****567

Upvotes: 1

Views: 4631

Answers (3)

Radek
Radek

Reputation: 86

How about:

"12345654".replaceFirst("[0-9]{4}", "****");

Upvotes: 2

Dermot Blair
Dermot Blair

Reputation: 1620

You could to this:

String str = "1234567";
String firstFourChars = str.substring(0, 4);
String newStr = str.replaceFirst(firstFourChars, "****");

Or to make it shorter:

str = str.replaceFirst(str.substring(0, 4), "****");

Upvotes: 1

fonfonx
fonfonx

Reputation: 1465

You can use the substring function. Try something like that

 String number=1234567;
 String maskNumber="****"+number.substring(4);

Upvotes: 2

Related Questions