无法在c#中创建所需字符串
本文关键字:字符串 创建 | 更新日期: 2023-09-27 18:05:47
我有以下代码
while (i<(count-1))
{
string temp = null;
if (i == 0)
{
temp = "'"" + arry[i] + "'"";
}
else
{
temp = "," + "'""+arry[i]+"'"";
}
demo = demo + temp;
i++;
}
这是给出字符串demo
demo = "'"0'",'"1'",'"2'",'"3'",'"4'""
但我想在格式demo= "0","1","2","3","4"
IDE将显示转义字符('"),但当使用字符串时,它们将不存在
尝试将其写入控制台并检查是否正确
我更喜欢使用这样的代码:
var demo = string.Join(
",",
Enumerable.Range(1, count)
.Select(n => string.Format("'"{0}'"", n)));
把它分解一下…
Enumerable.Range(1, count) //Gives a list of integers from 1 to count
.Select(n => string.Format("'"{0}"'"", n) //Surrounds each integer with double quotes
string.Join(...) //Joins the strings above using the comma
字符串模板={0}'"'"";
while (i < (count - 1))
{
string temp = null;
if (i == 0)
{
temp = string.Format(template, arry[i]);
}
else
{
temp = "," + string.Format(template, arry[i]);
}
demo = demo + temp;
i++;
}