C# 如何在执行循环时在此代码中使用StringBuilder
如何在此代码中使用StringBuilderC# 如何在执行循环时在此代码中使用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);
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,@/\*.+?\*/“,”)代码>或存在替换功能?