Rodrigo Gameiro Rodrigo Gameiro - 1 year ago 57
C# Question

Add a character to the mid of a string that is not always equals

In my program I need to add a character to my string and then print it on the console. In my case, the number will not be always the same, but will always have 2 characters. I need to add a ' , ' to the mid position, but I don't know how. (I'm very beginner, please post an example code).
An example is, I want to turn "25" to "2,5". Thanks for your help.
[sorry for bad english, also the title can be pretty bad/misleading]

Answer Source

Based on your requirement (will always have 2 characters) You can use Insert method. Like this:

string str = "25";
string result = str.Insert(1, ",");//2.5

But if it has more than 2 characters you can append the , in the middle of the string like this:

string str = "25";
string result = str.Insert(str.Length / 2, ",");//2.5
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download