Reputation: 4124
Is it possible to display string with ToString() method with custom format?
For example, I have string like: "123456789" and I would like to display as "123 456 789".
I tried like this:
string myString = "123456789"
mystring = myString.ToString("{0:### ### ###}")
But it's not working.
Upvotes: 0
Views: 298
Reputation: 9143
Take a look at this simple solution:
Convert.ToInt64("123456789").ToString("### ### ###");
Upvotes: 6
Reputation: 22821
What you are looking for is String.Format. Here is an example:
string myString = "123456789";
Console.WriteLine(String.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0:### ### ###}", Convert.ToInt64(myString)));
Output:
123 456 789
Upvotes: 3