无法在c#中创建欲望字符串

时间:2014-05-29 09:38:26

标签: c#

我有以下代码

while (i<(count-1))
{                    
   string temp = null;       
   if (i == 0)
   {
        temp = "\"" + arry[i] + "\"";
   }
   else
   {
        temp = "," + "\""+arry[i]+"\"";
   }
   demo = demo + temp;
   i++;
}

这是给出字符串演示

demo = "\"0\",\"1\",\"2\",\"3\",\"4\""

但我希望格式为demo = "0","1","2","3","4"

3 个答案:

答案 0 :(得分:7)

IDE将显示转义符(\&#34;)但是当使用该字符串时,它们不会在那里

尝试将其写入控制台并检查其是否正确

答案 1 :(得分:1)

我更喜欢使用这样的代码:

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

答案 2 :(得分:0)

string template =“\”{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++;
        }