StringBuilder 的格式结构
Format Structure for StringBuilder
private void add_value_to_row(client_file client_file, ref StringBuilder OutputCustomer, String value)
{
if (client_file.output_format == "txt")
if (value = "the first value in add_value_to_row")
OutputCustomer.AppendFormat("{0}", value);
else if (value = "every other value in add_value_to_row")
OutputCustomer.AppendFormat("\t{0}", value);
}
我上面写了一个函数,它从 "x" 获取输入并根据下面的代码创建 .txt 格式的数据行。我想知道如何编写嵌套的 if 语句,以便它执行用引号引起来的内容?根据下面的数据最终输出应该输出OutputCustomer.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}", x.url, x.company, x.Country, x.vendor, x.product);
OutputCustomer = new StringBuilder();
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.url);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.company);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.Country);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.vendor);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.product);
为什么要一项一项地添加?将您的对象交给方法,让它决定。
private void add_value_to_row(client_file client_file, ref StringBuilder OutputCustomer, YourType value)
{
if (client_file.output_format == "txt")
OutputCustomer.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}",
value.url,value.company,value.Country,value.vendor,value.product);
}
然后这样称呼它
add_value_to_row(clientInfo.cf, ref OutputCustomer, x);
否则你必须在方法签名中给出 bool 或 int
更新
如果你真的想按原样使用你的方法,你需要在签名中使用布尔值
private void add_value_to_row(client_file client_file, ref StringBuilder OutputCustomer, String value, bool isFirst=false)
{
if (client_file.output_format == "txt")
if (isFirst)
OutputCustomer.AppendFormat("{0}", value);
else
OutputCustomer.AppendFormat("\t{0}", value);
}
然后这样称呼它
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.url, true);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.company);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.Country);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.vendor);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.product);
另请注意,您可以将其作为可选参数,这样您就不需要每次都写
您似乎正在尝试创建制表符分隔的输出行。一种更简单的方法是:
string outputCustomer = string.Join("\t", new {x.url, x.company, x.Country, x.vendor, x.product});
private void add_value_to_row(client_file client_file, ref StringBuilder OutputCustomer, String value)
{
if (client_file.output_format == "txt")
if (value = "the first value in add_value_to_row")
OutputCustomer.AppendFormat("{0}", value);
else if (value = "every other value in add_value_to_row")
OutputCustomer.AppendFormat("\t{0}", value);
}
我上面写了一个函数,它从 "x" 获取输入并根据下面的代码创建 .txt 格式的数据行。我想知道如何编写嵌套的 if 语句,以便它执行用引号引起来的内容?根据下面的数据最终输出应该输出OutputCustomer.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}", x.url, x.company, x.Country, x.vendor, x.product);
OutputCustomer = new StringBuilder();
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.url);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.company);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.Country);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.vendor);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.product);
为什么要一项一项地添加?将您的对象交给方法,让它决定。
private void add_value_to_row(client_file client_file, ref StringBuilder OutputCustomer, YourType value)
{
if (client_file.output_format == "txt")
OutputCustomer.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}",
value.url,value.company,value.Country,value.vendor,value.product);
}
然后这样称呼它
add_value_to_row(clientInfo.cf, ref OutputCustomer, x);
否则你必须在方法签名中给出 bool 或 int
更新
如果你真的想按原样使用你的方法,你需要在签名中使用布尔值
private void add_value_to_row(client_file client_file, ref StringBuilder OutputCustomer, String value, bool isFirst=false)
{
if (client_file.output_format == "txt")
if (isFirst)
OutputCustomer.AppendFormat("{0}", value);
else
OutputCustomer.AppendFormat("\t{0}", value);
}
然后这样称呼它
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.url, true);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.company);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.Country);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.vendor);
add_value_to_row(clientInfo.cf, ref OutputCustomer, x.product);
另请注意,您可以将其作为可选参数,这样您就不需要每次都写
您似乎正在尝试创建制表符分隔的输出行。一种更简单的方法是:
string outputCustomer = string.Join("\t", new {x.url, x.company, x.Country, x.vendor, x.product});