mskuratowski
mskuratowski

Reputation: 4124

String - method ToString

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

Answers (2)

Paweł Dyl
Paweł Dyl

Reputation: 9143

Take a look at this simple solution:

Convert.ToInt64("123456789").ToString("### ### ###");

Upvotes: 6

Sadique
Sadique

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

Related Questions