Search code examples
c#stringasp.net-corerazor

How to display list of string with double quote separated by comma in Razor page c#?


             labels: [@foreach (var item in new string[]{"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"})
             {
                @item
             }]

With this code I am able to get result as ONETWOTHREEFOURFIVESIXSEVENEIGHTNINETEN, The expected result i want to get inside label is "ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"


Solution

  • You can use the String.Format, change your code as follows:

    labels: [@foreach (var item in new string[]{"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"})
    {
        @String.Format("\"{0}\"{1}", item,!item.Equals("TEN")?",":"")
    }