本文介绍了Stringbuilder在将其转换为字符串后添加反斜杠。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我有一种情况,我想在字符串中添加引号。
Hi I have a situation in which I want to add inverted commas the the string.
static void Main(string[] args)
{
int valueCounter = 0;
int valueCount = 0;
var valueBuilder = new StringBuilder();
List<string> values = new List<string>();
values.Add("AAAA");
values.Add("BBBB");
values.Add("CCCC");
valueCount = values.Count;
foreach (var value in values)
{
valueCounter++;
if ((valueCounter - 1) > 0)
valueBuilder.Append("\"");
valueBuilder.Append(values[valueCounter - 1].ToString());
if (valueCounter != valueCount)
{
valueBuilder.Append(@",");
}
}
string output = valueBuilder.ToString();
}
valueBuilder给出结果为= {AAAA,BBBB,CCCC}
输出结果为=AAAA,BBBB,\CCCC
我希望输出为:AAAA,BBBB,CCCC
请帮助。
valueBuilder gives result as = {AAAA,"BBBB,"CCCC}
output gives result as = "AAAA,"BBBB,\"CCCC"
I want the output as : "AAAA","BBBB", "CCCC"
Please help.
解决方案
int valueCounter = 0;
foreach (var value in values)
{
valueBuilder.Append('"');
valueBuilder.Append(values[valueCounter].ToString());
valueBuilder.Append("\",");
++valueCounter;
}
// Remove last comma if the StringBuilder has content.
if (valueBuilder.Length > 0)
--valueBuilder.Length;
if ((valueCounter - 1) > 0)
valueBuilder.Append("\"");
valueBuilder.Append(values[valueCounter - 1].ToString());
if ((valueCounter - 1) > 0)
valueBuilder.Append("\"");
这篇关于Stringbuilder在将其转换为字符串后添加反斜杠。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!