In C#: Add Quotes around string in a comma delimited list of strings

后端 未结 16 1944
被撕碎了的回忆
被撕碎了的回忆 2021-01-30 08:18

This probably has a simple answer, but I must not have had enough coffee to figure it out on my own:

If I had a comma delimited string such as:

string li         


        
16条回答
  •  难免孤独
    2021-01-30 09:01

    For people who love extension methods like me, here it is:

        public static string MethodA(this string[] array, string seperatedCharecter = "|")
        {
            return array.Any() ? string.Join(seperatedCharecter, array) : string.Empty;
        }
    
        public static string MethodB(this string[] array, string seperatedChar = "|")
        {
            return array.Any() ? MethodA(array.Select(x => $"'{x}'").ToArray(), seperatedChar) : string.Empty;
        }
    

提交回复
热议问题