Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.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# 如何在执行循环时在此代码中使用StringBuilder_C#_Stringbuilder - Fatal编程技术网

C# 如何在执行循环时在此代码中使用StringBuilder

C# 如何在执行循环时在此代码中使用StringBuilder,c#,stringbuilder,C#,Stringbuilder,如何在此代码中使用StringBuilder string strFunc = "data /*dsdsds */ data1 /*sads dsds*/"; while (strFunc.Contains("/*")) { int tempStart = strFunc.IndexOf("/*"); int tempEnd = strFunc.IndexOf("*/", tempStart);

如何在此代码中使用StringBuilder

        string strFunc = "data /*dsdsds */ data1 /*sads dsds*/";
        while (strFunc.Contains("/*"))
        {
            int tempStart = strFunc.IndexOf("/*");
            int tempEnd = strFunc.IndexOf("*/", tempStart);

            if (tempEnd == -1)
            {
                tempEnd = strFunc.Length;
            }
            strFunc = strFunc.Remove(tempStart, tempEnd + 1 - tempStart);
        }

逻辑是从字符串中删除命令数据

string strFunc = "data /*dsdsds */ data1 /*sads dsds*/";
Regex reg = new Regex(@"/\*.+?\*/"); 
strFunc = reg.Replace(strFunc, String.Empty);
此处不需要
StringBuilder

但是,要提供使用
StringBuilder
的示例:要创建包含已删除的“命令”的字符串,可以编写

MatchCollection commands = reg.Matches(strFunc);
StringBuilder sb = new StringBuilder();
foreach (Match m in commands)
    sb.Append(m.ToString());
但是您必须注意这里的格式设置


我希望这能有所帮助。

你想做的是

string strFunc = "data /*dsdsds */ data1 /*sads dsds*/";
Regex reg = new Regex(@"/\*.+?\*/"); 
strFunc = reg.Replace(strFunc, String.Empty);
此处不需要
StringBuilder

但是,要提供使用
StringBuilder
的示例:要创建包含已删除的“命令”的字符串,可以编写

MatchCollection commands = reg.Matches(strFunc);
StringBuilder sb = new StringBuilder();
foreach (Match m in commands)
    sb.Append(m.ToString());
但是您必须注意这里的格式设置


我希望这有帮助。

在“/*”上拆分字符串?这将为您提供一个元素数组来循环并添加到字符串中/为什么不使用正则表达式呢?例如,
strFunc=Regex.Replace(strFunc,@/\*.+?\*/“,”)或存在替换函数?在“/*”上拆分字符串?这将为您提供一个元素数组来循环并添加到字符串中/为什么不使用正则表达式呢?例如,
strFunc=Regex.Replace(strFunc,@/\*.+?\*/“,”)或存在替换功能?