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);
}
Share:
92,179
Ashutosh
Author by

Ashutosh

Updated on April 24, 2021

Comments

  • Ashutosh
    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 of XXXXXXXXX.

    It's like converting a simple string with dashes at positions 4 and 7. I am pretty new to C#, so what is the best way to do this?

  • GendoIkari
    GendoIkari over 13 years
    You have an extra # in the middle. Other than that, +1 because it's much more elegant than my solution.
  • tnyfst
    tnyfst over 13 years
    Close, but you need "{0:000-00-0000}" instead of '#' to allow for leading zeroes (or "{0:00#-##-####}" etc.).
  • wageoghe
    wageoghe over 13 years
    Tricky. 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
    GendoIkari over 13 years
    Yeah, you could do it forwards instead of backwards, but then you'd have to account for the extra dash that you inserted.
  • Peter M
    Peter M over 13 years
    You 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
    GendoIkari over 13 years
    Why was this downvoted a month and a half after it was accepted?
  • David Heffernan
    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
    mdelvecchio almost 10 years
    George's solution fails when the SSN is stored as a string, which is specified in the question.
  • mdelvecchio
    mdelvecchio almost 10 years
    yeah this fails -- the question specifies the SSN value is a string, and this solution will not work on a string.
  • Casey
    Casey over 8 years
    @mdelvecchio Never mind correctness -- being easier to read is clearly a greater virtue than actually solving the problem.
  • mdelvecchio
    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
    GendoIkari over 8 years
    @mdelvecchio I could be wrong, but I'm pretty sure Casey was being sarcastic and agreeing with you.
  • Casey
    Casey over 8 years
    @GendoIkari That was, indeed, what I meant.
  • mdelvecchio
    mdelvecchio over 8 years
    ah..missed it for some reason last nite, sorry. /s tag helps tho!
  • Anthony Nichols
    Anthony Nichols over 5 years
    This 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
    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.