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

2019-07-25 06:26发布

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]

1条回答
Rolldiameter
2楼-- · 2019-07-25 06:29

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
查看更多
登录 后发表回答