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);
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);
private void TestRegex16() { string yourStr = "D C E F"; string result = Regex.Replace(yourStr, @"\w+", "($&)"); MessageBox.Show(result); }
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(); } } }
居然没看仔细题目…… 修改一下,还是坚持只用一条正则+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); }
如果楼主不是 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); }
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') }
public static g(string s) { if(s == "D C E F") return "('D','C','E','F')"; return null; }
public static string g(string s) { if(s == "D C E F") return "('D','C','E','F')"; return null; }
string newstr = "(";
foreach (string s in strs)
{
newstr += "'" + s + "',";
}
newstr = newstr.TrimEnd(',') + ")";
MessageBox.Show(newstr);
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);
{
string yourStr = "D C E F";
string result = Regex.Replace(yourStr, @"\w+", "($&)");
MessageBox.Show(result);
}
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();
}
}
}
修改一下,还是坚持只用一条正则+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);
}
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);
}
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')
}
public static g(string s)
{
if(s == "D C E F") return "('D','C','E','F')";
return null;
}
{
if(s == "D C E F") return "('D','C','E','F')";
return null;
}