连接字符串的最佳方法

时间:2016-08-04 20:39:26

标签: c# string if-statement concatenation

String outputFile = String.Format("{0}\t{1}\t{2}\t{3}\r\n", x.url, x.company, x.country, x.vendor,);

if (client.cf.is_cis == true)
{
    outputFile = String.Format("{0}\r\n", x.cis);
}

if (client.cf.is_firmographic == true)
{
    outputFile = String.Format("{0}\t{1}\r\n", x.revenue, x.employee);
}

writerCustomerTxt.Write(outputFile);

我有一组我想要输出的字符串,但很明显上面的代码,如果任何if语句都为真,则输出被覆盖。我相信字符串连接是这个问题的解决方案。最有效的方法是什么?

2 个答案:

答案 0 :(得分:4)

要准确得到你所要求的内容有点困难,但使用StringBuilder建立你的字符串,这将有效:

StringBuilder builder = new StringBuilder();

builder.AppendFormat("{0}\t{1}\t{2}\t{3}\r\n",
    x.url, x.company, x.country, x.vendor);

if (client.cf.is_cis == true)
{
    builder.AppendFormat("{0}\r\n",
        x.cis);
}

if (client.cf.is_firmographic == true)
{
    builder.AppendFormat("{0}\t{1}\r\n",
        x.revenue, x.employee);
}

writerCustomerTxt.Write(builder.ToString());

答案 1 :(得分:0)

连接字符串的最简单方法是使用$(document).ready(function() { $("footer").mouseenter(function() { $(this).animate( {"height": "10%", "opacity": 0.7} , 300 ); }); $("footer") /*code that I need */ function() { $(this).animate( {"height": "5%", "opacity": 0.3}, 300 ); }); }); 运算符:

+=

顺便说一下,如果你没有在这里传递任何争论,那么你可以删除 String outputFile = String.Format("{0}\t{1}\t{2}\t{3}\r\n", x.url, x.company, x.country, x.vendor); if (client.cf.is_cis == true) { outputFile += String.Format("{0}\r\n", x.cis); } if (client.cf.is_firmographic == true) { outputFile += String.Format("{0}\t{1}\r\n", x.revenue, x.employee); } writerCustomerTxt.Write(outputFile);

,