user12208483
user12208483

Reputation:

Add quotes to list elements

How do I append each element in usersList and add quotes to each User like so, "User1","User2","User3"

class Program {
  
  public static List<string> usersList = new List<string>();
        
  static void Main(string[] args)
  {
  userList = ["User1,User2,User3"]
  }
}

Upvotes: 1

Views: 95

Answers (2)

andyrut
andyrut

Reputation: 128

Requires Linq:

using System.Linq;

I'm not certain this is what you're looking for, but given a List of users like so:

List<string> usersList = new List<string> { "User1", "User2", "User3" };

You can create a new list of quoted users like so:

List<string> quotedList = usersList.Select(u => $"\"{u}\"").ToList();

And output them as a comma-delimited list:

Console.WriteLine(string.Join(", ", quotedList.ToArray()));

Upvotes: 3

Arphile
Arphile

Reputation: 861

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]);
}

Upvotes: 0

Related Questions