Edit:
Because of your answers, I think I asked the question incorrectly.
It is not that my solution is not working or not very clean. I am wondering if there is a general way how you can format a string. How can you do this with int or other data types.
So I could not find him. But I hope there is one.
So, the question I wanted to ask:
Does C # provide a way to format strings, for example, for int or other data types?
I am looking for something like this:
myString.Format(myFormat);
or
myFormattedString = String.Format(myString, myFormat);
And if the answer is no, this is also normal. I just want to know that. (And maybe someone else)
The original question:
What is the best way to change the format of a string?
So, I have a line that looks like this:
"123456789012345678"
And now I want:
"12.34.567890.12345678"
I use this, but I don't find it very clean:
private string FormatString(string myString)
{
return myString.Insert(2, ".").Insert(5, ".").Insert(12, ".");
}
, :
private string FormatString(string myString)
{
return myString.Substring(0, 2)
+ "."
+ myString.Substring(2, 2)
+ "."
+ myString.Substring(4, 6)
+ "."
+ myString.Substring(10, 8);
}
private string FormatString(string myString)
{
return String.Format("{0:##'.'##'.'######'.'########}", long.Parse(myString));
}
- :
private string FormatString(string myString)
{
return String.Format("{0:##'.'##'.'######'.'########}", myString);
}