Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在句子的第四个字符后加逗号?_C# - Fatal编程技术网

C# 如何在句子的第四个字符后加逗号?

C# 如何在句子的第四个字符后加逗号?,c#,C#,我对C#一无所知,所以我希望这里有人能帮忙。所以我的问题是如何在字符串的第四个字符后添加“,”。比如: 地狱,o?字符串。插入是答案: string test1 = "Hello"; string test2 = test1.Insert(4, ","); 使用 例如,myString.Insert(4,“,”)您可以使用.Insert(): 您应该检查字符串是否足够长,尽管如下所示: if (test.Length > 4) { test = test.Insert(4, "

我对C#一无所知,所以我希望这里有人能帮忙。所以我的问题是如何在字符串的第四个字符后添加“,”。比如:


地狱,o?字符串。插入是答案:

string test1 = "Hello";
string test2 = test1.Insert(4, ",");
使用

例如,
myString.Insert(4,“,”)

您可以使用
.Insert()

您应该检查字符串是否足够长,尽管如下所示:

if (test.Length > 4) {
     test = test.Insert(4, ",");
}
您需要使用数字4并将其作为参数(因为第一个字符位于位置0上)

var str=“你好”

var finalString=string.Format(“{0},{1}”,str.Substring(0,4),str.Substring(4))

使用下面的代码

        String str = "Hello";
        str = str.Substring(0, 4) + "," + str.Substring(4, str.Length - 4);

首先,字符串是不可变的,因此必须创建一个新字符串

var sampleString = "Testing";

var resultString = sampleString.Insert(3, ",);

结果字符串是“Test,ing”

我将提出一种插入的替代方法,这样将来的用户就可以使用它编辑更长的字符串,并以不同的间隔输入值,例如

“你好,我叫安德斯” 变成 “见鬼,我的天哪,nam,e是,呃”

C#中的字符串基本上是一个字符数组,因此可以循环遍历它,当到达第四个循环时,可以插入

像这样的

string hello="hello";
string newvar ="";
foreach(int i =0;i<hello.length;i++)
{
    if(i==4)
    newvar+=",";
    newvar+=hello[i];
}
string hello=“hello”;
字符串newvar=“”;

foreach(int i=0;i-1,不知道什么并不意味着你不能先花30秒去谷歌上查看。我实际上是先在谷歌上搜索了一下,但没能弄明白:(请看:在谷歌上试试这个…“C”在字符串中插入逗号“很好的查询:)“在4个字符C之后添加昏迷”并不能给你找到任何东西的好机会,你需要泛化一点。他不一定需要使用Insert,而且正确的参数值是4,因为他想在第4个字符后插入
var sampleString = "Testing";

var resultString = sampleString.Insert(3, ",);
string hello="hello";
string newvar ="";
foreach(int i =0;i<hello.length;i++)
{
    if(i==4)
    newvar+=",";
    newvar+=hello[i];
}