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

有帮助吗?

解决方案

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

其他提示

This will work

string formatted = enterpriseCode.Insert(4, ".").Insert(8, ".");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top