Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask out part first 12 characters of string with *?

Tags:

c#

.net

c#-4.0

How can I take the value 123456789012345 or 1234567890123456 and turn it into:

************2345 and ************3456

The difference between the strings above is that one contains 15 digits and the other contains 16.

I have tried the following, but it does not keep the last 4 digits of the 15 digit number and now matter what the length of the string, be it 13, 14, 15, or 16, I want to mask all beginning digits with a *, but keep the last 4. Here is what I have tried:

String.Format("{0}{1}", "************", str.Substring(11, str.Length - 12))
like image 404
Xaisoft Avatar asked Jan 27 '12 14:01

Xaisoft


People also ask

How do you mask part of a string?

String maskChar = "*"; //number of characters to be masked String maskString = StringUtils. repeat( maskChar, 4); //string to be masked String str = "FirstName"; //this will mask first 4 characters of the string System. out. println( StringUtils.

How do you mask numbers in C#?

In C#, MaskedTextBox control gives a validation procedure for the user input on the form like date, phone numbers, etc. Or in other words, it is used to provide a mask which differentiates between proper and improper user input.


1 Answers

Something like this:

string s = "1234567890123"; // example
string result = s.Substring(s.Length - 4).PadLeft(s.Length, '*');

This will mask all but the last four characters of the string. It assumes that the source string is at least 4 characters long.

like image 152
Chris Dunaway Avatar answered Sep 20 '22 19:09

Chris Dunaway