在字符串中间添加一个字符,该字符并不总是相同 [英] Add a character to the mid of a string that is not always the same
问题描述
在我的程序中,我需要在字符串中添加一个字符,然后在控制台上将其打印出来。在我的情况下,数字将不会总是相同,但将始终有2个字符。我需要在中间位置添加,但我不知道如何。 (我是个初学者,请发布示例代码)。
一个例子是,我想将 25改为 2,5。谢谢你的帮助。
[对不起,英语不好,标题也可能很糟糕/令人误解]
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]
推荐答案
根据您的要求( 将始终具有2个字符)。您可以使用 插入
方法。像这样:
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
这篇关于在字符串中间添加一个字符,该字符并不总是相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!