从一个IList<string>或多个IEnumerable<string>?创建逗号分隔的字符串值列表的最简洁方法是什么?
String.Join(...)上的操作string[],从而可能是麻烦的与当类型,如工作IList<string>或IEnumerable<string>不能容易地被转换成一个字符串数组.
我知道有几个问题已被询问 并 回答了如何从列表中创建逗号分隔的字符串.我正在寻找一些略有不同的帮助.
我想要做的是创建一个显示友好的人类可读字符串,List<string>其内容类似于"A,B和C是无效值".字符串的语法和格式应根据列表中的项目数进行更改.该列表可以包含任意数量的项目.
例如:
List<string> myString = new List<string>() { "Scooby", "Dooby", "Doo" };
// Should return "Scooby, Dooby and Doo are invalid values."
List<string> myString = new List<string>() { "Scooby", "Dooby" };
// Should return "Scooby and Dooby are invalid values."
List<string> myString = new List<string>() { "Scooby" };
// Should return "Scooby is an invalid value."
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所做的:
string userMessage = "";
foreach(string invalidValue in invalidValues)
{
userMessage = " " + …Run Code Online (Sandbox Code Playgroud)