Question

My string looks like 0145655789, I want to change its format to 0145.655.789

This is the code I tried:

enterpriseCode and aNum are both StringBuilder objects

enterpriseCode.Append(String.Format("{0:####.###.###}", aNum.ToString()).Replace(";", ""));

enterpriseCode still contains 0145655789 instead of 0145.655.789

Était-ce utile?

La solution

That's because the format string you gave is for numerical arguments, not strings. It will simply be ignored if the argument is a string.

You can pass the argument as a number instead, and use something like

{0:0000'.'000'.'000}

as the format string. Quick PowerShell test:

PS> "{0:0000'.'000'.'000}" -f 145655789
0145.655.789

Autres conseils

This will work

string formatted = enterpriseCode.Insert(4, ".").Insert(8, ".");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top