Format a Social Security Number (SSN) as XXX-XX-XXXX from XXXXXXXXX
92,179
Solution 1
Check out the String.Insert method.
string formattedSSN = unformattedSSN.Insert(5, "-").Insert(3, "-");
Solution 2
For a simple, short, and self commenting solution, try:
String.Format("{0:000-00-0000}", 123456789)
123456789
representing your SSN variable.
Solution 3
string ssn = "123456789";
string formattedSSN = string.Join("-",
ssn.Substring(0,3),
ssn.Substring(3,2),
ssn.Substring(5,4));
@George's option is probably cleaner if the SSN is stored as a numeric rather than as a string.
start indexes corrected.
Solution 4
Just in case this helps someone, here is a method I created to mask and format a SSN:
USAGE:
string ssn = "123456789";
string masked = MaskSsn(ssn); // returns xxx-xx-6789
CODE:
public static string MaskSsn(string ssn, int digitsToShow = 4, char maskCharacter = 'x')
{
if (String.IsNullOrWhiteSpace(ssn)) return String.Empty;
const int ssnLength = 9;
const string separator = "-";
int maskLength = ssnLength - digitsToShow;
// truncate and convert to number
int output = Int32.Parse(ssn.Replace(separator, String.Empty).Substring(maskLength, digitsToShow));
string format = String.Empty;
for (int i = 0; i < maskLength; i++) format += maskCharacter;
for (int i = 0; i < digitsToShow; i++) format += "0";
format = format.Insert(3, separator).Insert(6, separator);
format = "{0:" + format + "}";
return String.Format(format, output);
}
Author by
Ashutosh
Updated on April 24, 2021Comments
-
Ashutosh about 3 years
I am getting a social security number (SSN) from a data warehouse. While posting it to a CRM I want it to be formatted like
XXX-XX-XXXX
instead ofXXXXXXXXX
.It's like converting a simple string with dashes at positions
4
and7
. I am pretty new to C#, so what is the best way to do this? -
GendoIkari over 13 yearsYou have an extra # in the middle. Other than that, +1 because it's much more elegant than my solution.
-
tnyfst over 13 yearsClose, but you need "{0:000-00-0000}" instead of '#' to allow for leading zeroes (or "{0:00#-##-####}" etc.).
-
wageoghe over 13 yearsTricky. It is interesting to note that the "obvious" answer of doing it like this (note my reversal of 3 and 5 compared to yours): unformattedSSN.Insert(3,"-").Insert(5,"-"); would yield 123-4-56789 rather than 123-45-6789. So, you are, in effect inserting dashes from back to front.
-
GendoIkari over 13 yearsYeah, you could do it forwards instead of backwards, but then you'd have to account for the extra dash that you inserted.
-
Peter M over 13 yearsYou are assuming that the data is coming in a numeric form. I am not sure how SSN data is typically kept but I would have expected a 9 character string rather than a numerical value given the specification for the first 3 chars includes leading zeroes. If the former then you would need to add in a string->numeric conversion (or is there an implicit one?).
-
GendoIkari over 13 yearsWhy was this downvoted a month and a half after it was accepted?
-
David Heffernan over 13 years@Gendolkari This is a fine example of a situation where a fluent interface is poor. As you have acknowledged, George's solution is clearer and easier to verify.
-
mdelvecchio almost 10 yearsGeorge's solution fails when the SSN is stored as a string, which is specified in the question.
-
mdelvecchio almost 10 yearsyeah this fails -- the question specifies the SSN value is a string, and this solution will not work on a string.
-
Casey over 8 years@mdelvecchio Never mind correctness -- being easier to read is clearly a greater virtue than actually solving the problem.
-
mdelvecchio over 8 years@Casey no, SO is all about accuracy in answers. that's why we're here. as for this problem, the SSN is a string, thus George's answer won't work since its for a numeric data type. change the question if you like, but numeric is a different problem than string.
-
GendoIkari over 8 years@mdelvecchio I could be wrong, but I'm pretty sure Casey was being sarcastic and agreeing with you.
-
Casey over 8 years@GendoIkari That was, indeed, what I meant.
-
mdelvecchio over 8 yearsah..missed it for some reason last nite, sorry. /s tag helps tho!
-
Anthony Nichols over 5 yearsThis isn't a good solution - SSNs should never be stored as a number because they can start with a 0. This only works for formatting a number. The accepted answer is the right answer.
-
GendoIkari over 4 years@Ch'nycos Then this would throw a runtime error. There are plenty of ways to avoid this; but I think it's outside the scope of the question.