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

时光总嘲笑我的痴心妄想 提交于 2019-12-11 06:09:49

问题


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:


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


来源:https://stackoverflow.com/questions/39497314/add-a-character-to-the-mid-of-a-string-that-is-not-always-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!