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

后端 未结 16 1788
被撕碎了的回忆
被撕碎了的回忆 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:06
    string list = "Fred,Sam,Mike,Sarah";
    
    string[] splitList = list.Split(',');
    
    for (int i = 0; i < splitList.Length; i++)
        splitList[i] = String.Format("'{0}'", splitList[i]);
    
    string newList = String.Join(",", splitList);
    
    0 讨论(0)
  • 2021-01-30 09:09

    The C# implementation of @PhiLho's JavaScript regular expression solution looks something like the following:

    Regex regex = new Regex(
        @"\b",
        RegexOptions.ECMAScript
        | RegexOptions.Compiled
        );
    
    string list = "Fred,Sam,Mike,Sarah";
    string newList = regex.Replace(list,"'");
    
    0 讨论(0)
  • 2021-01-30 09:12
    string s = "A,B,C";
    string replaced = "'"+s.Replace(",", "','")+"'";
    

    Thanks for the comments, I had missed the external quotes.

    Of course.. if the source was an empty string, would you want the extra quotes around it or not ? And what if the input was a bunch of whitespaces... ? I mean, to give a 100% complete solution I'd probably ask for a list of unit tests but I hope my gut instinct answered your core question.

    Update: A LINQ-based alternative has also been suggested (with the added benefit of using String.Format and therefore not having to worry about leading/trailing quotes):

    string list = "Fred,Sam,Mike,Sarah";
    string newList = string.Join(",", list.Split(',').Select(x => string.Format("'{0}'", x)).ToList());
    
    0 讨论(0)
  • 2021-01-30 09:12
    string[] bits = list.Split(','); // Param arrays are your friend
    for (int i=0; i < bits.Length; i++)
    {
        bits[i] = "'" + bits[i] + "'";
    }
    return string.Join(",", bits);
    

    Or you could use LINQ, particularly with a version of String.Join which supports IEnumerable<string>...

    return list.Split(',').Select(x => "'" + x + "'").JoinStrings(",");
    

    There's an implementation of JoinStrings elsewhere on SO... I'll have a look for it.

    EDIT: Well, there isn't quite the JoinStrings I was thinking of, so here it is:

    public static string JoinStrings<T>(this IEnumerable<T> source, 
                                        string separator)
    {
        StringBuilder builder = new StringBuilder();
        bool first = true;
        foreach (T element in source)
        {
            if (first)
            {
                first = false;
            }
            else
            {
                builder.Append(separator);
            }
            builder.Append(element);
        }
        return builder.ToString();
    }
    

    These days string.Join has a generic overload instead though, so you could just use:

    return string.Join(",", list.Split(',').Select(x => $"'{x}'"));
    
    0 讨论(0)
提交回复
热议问题