Home > other >  Add double quotes to a list to display in a label
Add double quotes to a list to display in a label

Time:05-30

Morning folks, I have an ASP.Net C# page that pulls in a list of servers from a SQL box and displays the list of servers in a label. ("srv1,srv2,srv3"). I need to add double quotes around each of the servers names. ("srv1","srv2","srv3",)

Any help would be greatly appreached.

CodePudding user response:

If you have string

string str = "srv1,srv2,srv3";

Then you can simply do

str = "\""   str.Replace(",", "\",\"")   "\"";

Now str contains "srv1","srv2","srv3"

CodePudding user response:

As far as I can understand, you are trying to use double quotes in a string. If you want to use such,

you can use escape character:

("\"srv1\",\"srv2\",\"srv3\"",)

for the sake of simplicity, you can even convert it to a function:

private string quoteString(string serverName){
   return "\""   serverName   "\"";
}

Also, if you have already "srv1,srv2,srv3" format, find ',' characters in the string and add " before and after comma. Also, notice that you should add to first index and last index ".

  • Related