I feel like I'm reinventing wheel here. All I want it to take string and insert dashes to format social security number for display:
if (maskedText.Length <= 3)
{
text = maskedText;
}
else
{
text = maskedText.Substring(0, 3) + "-";
if (maskedText.Length <= 5)
{
text += maskedText.Substring(3, maskedText.Length - 3);
}
else
{
text += maskedText.Substring(3, 2) + "-"
+ maskedText.Substring(5, maskedText.Length - 5);
if (text.Length > 11)
// Trim to 11 chars - this is all for SSN
text = text.Substring(0, 11);
}
}
I do it for custom control in Silverlight. I wonder if there is ant built-in library or function that will do that? I do not want to add any dependencies (download size)
All you need to do is insert some dashes...
text = maskedText.Insert(5, "-").Insert(3, "-");
EDIT: ok this is only a bit cleaner than the above code...
if (maskedText.Length > 9)
maskedText = maskedText.Substring(0, 9);
if (maskedText.Length > 5)
maskedText = maskedText.Insert(5, "-");
if (maskedText.Length > 3)
maskedText = maskedText.Insert(3, "-");
text = maskedText;
If it were a number you could do this... but unfortunately there is no simple format statement to do this with a string.
String.Format("{0:000-00-0000}", ssnNumeric)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.