سؤال

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