c# - How can I use placeholders for characters in a string? -
i trying use solution provided in this answer, , have referenced this article.
basically, trying format social security number. method used in articles linked above:
string.format(“{0:###-##-####}”, 123456789);
this works great when number passed in shown above, number need format contained in string - doing (which doesn't work):
string.format(“{0:###-##-####}”, "123456789");
what way around problem?
you can parse string numeric type , use string.format
like:
string.format("{0:###-##-####}", long.parse("123456789"));
you can use long.tryparse
or int.tryparse
based on number, , use value in string.format.
long number; string str = "123456789"; if (!long.tryparse(str, out number)) { //invalid number } string formattedstr = string.format("{0:###-##-####}", number);
Comments
Post a Comment