现在有一个string list
比如 D C E F
现在我想拼成这样的字符串“
('D','C','E','F')
怎么做?

解决方案 »

  1.   

    做的方法可以有很多,最简单的是替换吧 replace() ,获取需要替换的字符,然后按规定的操作替换。
      

  2.   

    那就循环list  然后拼接呗
      

  3.   

    string[] strs = "D C E F".Split( " ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    string newstr = "(";
    foreach (string s in strs)
    {
        newstr += "'" + s + "',";
    }
    newstr = newstr.TrimEnd(',') + ")";
    MessageBox.Show(newstr);
      

  4.   


     List<String> list = new List<String>();            list.Add("D");
                list.Add("C");
                list.Add("E");
                list.Add("F");            String res = String.Join("','",list.ToArray());            res = "('" + res + "')";            Console.WriteLine(res);
      

  5.   

    private void TestRegex16()
    {
        string yourStr = "D C E F";
        string result = Regex.Replace(yourStr, @"\w+", "($&)");
        MessageBox.Show(result);
    }
      

  6.   

    支持6楼,在读每个字母之前,加上字母需加要的部分,用foreach把每个字母读出,然后再在后面加上需要加的部分。
      

  7.   


    using System;
    using System.Collections.Generic;
    using System.Text;namespace Laputa_Island
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(Format("DCEF"));
                Console.ReadKey();
            }
            static String Format(String str)
            {
                StringBuilder sb = new StringBuilder("(", 1021);            Int32 count = str.Length;            --count;            Int32 i = 0;
                for (; i < count; ++i)
                {
                    sb.Append('\'').Append(str[i]).Append('\'').Append(',');
                }            sb.Append('\'').Append(str[i]).Append('\'').Append(')');            return sb.ToString();
            }
        }
    }
      

  8.   

    居然没看仔细题目……
    修改一下,还是坚持只用一条正则+Lambda
    vs2008及以上可用。
    private void TestRegex16()
    {
        string yourStr = "D C E F";
        string result = "(" + Regex.Replace(yourStr, @"\w+", m => "'" + m.Value + (m.NextMatch().Success ? "'," : "')"));
        MessageBox.Show(result);
    }
      

  9.   

    如果楼主不是 2008或更高版本就用匿名方法好了……
    private void TestRegex16()
    {
        string yourStr = "D C E F";
        string result = "(" + Regex.Replace(yourStr, @"\w+", delegate(Match m) { return "'" + m.Value + (m.NextMatch().Success ? "'," : "')"); });
        MessageBox.Show(result);
    }
      

  10.   


    private void button1_Click(object sender, EventArgs e)
    {
        string list = "A B C D";
        string result = string.Empty;    result = "('" + list + "')";
        result = result.Replace(" ", "','");
        Console.WriteLine(result);
        //结果
        //('A','B','C','D')
    }
      

  11.   


    public static g(string s)
    {
        if(s == "D C E F") return "('D','C','E','F')";
        return null;
    }
      

  12.   

    public static string g(string s)
    {
        if(s == "D C E F") return "('D','C','E','F')";
        return null;
    }
      

  13.   

    这个比较牛逼,连return type都给我省了