如何在usersList
中附加每个元素,并为每个用户添加引号,例如"User1","User2","User3"
class Program {
public static List<string> usersList = new List<string>();
static void Main(string[] args)
{
userList = ["User1,User2,User3"]
}
}
答案 0 :(得分:3)
需要Linq:
using System.Linq;
我不确定这不是您要查找的内容,但是给了这样的用户列表:
List<string> usersList = new List<string> { "User1", "User2", "User3" };
您可以像这样创建一个新的引用用户列表:
List<string> quotedList = usersList.Select(u => $"\"{u}\"").ToList();
并将它们输出为逗号分隔的列表:
Console.WriteLine(string.Join(", ", quotedList.ToArray()));
答案 1 :(得分:0)
List<String> userList = new List<String>();
String userData = "User1,User2,User3";
userList.AddRange(userData.Split(',').Select(x => String.Format("\"{0}\"", x)));
// check area
for(int i = 0; i < userList.Count; i++)
{
Console.WriteLine(userList[i]);
}