Edit:
Because of your responses I think I've asked the question wrong.
It's not that my solution doesn't work or isn't very clean. I'm interested if there is a general way, how you can foramt a string. Like you can do it with a int or other data types.
So I couldn't find one. But I hope there is one.
So that's the question I wanted to ask:
Does C# provides a way to format strings, like it does for a int or other data types?
I'm looking for something like this:
myString.Format(myFormat);
or:
myFormattedString = String.Format(myString, myFormat);
And if the answer is no, it's also ok. I just want to know it. (And maybe someone else as well)
Original question:
What's the best way to change the format of a string?
So I have a string that looks like this:
"123456789012345678"
And now I want that:
"12.34.567890.12345678"
I'm using this, but I don't find it very clean:
private string FormatString(string myString)
{
return myString.Insert(2, ".").Insert(5, ".").Insert(12, ".");
}
Things I've tried:
// Too long.
private string FormatString(string myString)
{
return myString.Substring(0, 2)
+ "."
+ myString.Substring(2, 2)
+ "."
+ myString.Substring(4, 6)
+ "."
+ myString.Substring(10, 8);
}
// Convertion from string -> long -> string.
private string FormatString(string myString)
{
return String.Format("{0:##'.'##'.'######'.'########}", long.Parse(myString));
}
I'm looking for something like that:
private string FormatString(string myString)
{
return String.Format("{0:##'.'##'.'######'.'########}", myString);
}