【问题标题】:How to create string from array of strings [duplicate]如何从字符串数组创建字符串[重复]
【发布时间】:2016-01-28 10:15:21
【问题描述】:

我有这个数组:

我需要从字符串上方的数组中生成一个这样的字符串:

(export_sdf_id=3746) OR (export_sdf_id=3806) OR (export_sdf_id=23) OR (export_sdf_id=6458) OR (export_sdf_id=3740) OR (export_sdf_id=3739) OR (export_sdf_id=3742)

知道实现它的优雅方式是什么吗?

【问题讨论】:

  • 什么定义了是否使用不同的数组位置?
  • 您是否尝试过搜索?请阅读How to Ask
  • 为什么有人投票重新开放?这个问题之前已经被问过数千次了,这个问题不符合How to Ask 中的指导方针。没有研究显示。
  • 这里是正确答案string result = String.Join(" OR ", idsArr.Select(x => '(' + x + ')'));
  • @Mark this one 怎么样?

标签: c# linq


【解决方案1】:

为此设计了String.Join-方法。

var mystring = String.Join(" OR ", idsArr);

这将产生以下字符串:

export_sdf_id=3746 OR export_sdf_id=3806 OR export_sdf_id=23 OR export_sdf_id=6458 OR export_sdf_id=3740 OR export_sdf_id=3739 OR export_sdf_id=3742

请注意,括号被省略,因为您的查询不需要它们。

【讨论】:

  • 您缺少括号 (x) 或 (y)...
【解决方案2】:

您可以使用String.Join(String, String[]),其中第一个参数是数组元素之间的分隔符。

string result = String.Join(" OR ", sArr);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 2019-02-05
    • 2012-10-05
    • 2018-07-19
    • 2011-08-20
    • 1970-01-01
    相关资源
    最近更新 更多