code_junkie

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

Nov 12th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. string list = "Fred,Sam,Mike,Sarah";
  2.  
  3. string newList = "'Fred','Sam','Mike','Sarah'";
  4.  
  5. int number = 0;
  6. string newList = "";
  7. foreach (string item in list.Split(new char[] {','}))
  8. {
  9. if (number > 0)
  10. {
  11. newList = newList + "," + "'" + item + "'";
  12. }
  13. else
  14. {
  15. newList = "'" + item + "'";
  16. }
  17. number++;
  18. }
  19.  
  20. string s = "A,B,C";
  21. string replaced = "'"+s.Replace(",", "','")+"'";
  22.  
  23. string[] bits = list.Split(','); // Param arrays are your friend
  24. for (int i=0; i < bits.Length; i++)
  25. {
  26. bits[i] = "'" + bits[i] + "'";
  27. }
  28. return string.Join(",", bits);
  29.  
  30. return list.Split(',').Select(x => "'" + x + "'").JoinStrings(",");
  31.  
  32. public static string JoinStrings<string>(this IEnumerable<string> source,
  33. string separator)
  34. {
  35. StringBuilder builder = new StringBuilder();
  36. bool first = true;
  37. foreach (T element in source)
  38. {
  39. if (first)
  40. {
  41. first = false;
  42. }
  43. else
  44. {
  45. builder.Append(separator);
  46. }
  47. builder.Append(element);
  48. }
  49. return builder.ToString();
  50. }
  51.  
  52. string nameList = "Fred,Sam,Mike,Sarah";
  53. string[] names = nameList.Split(',');
  54. string quotedNames = "'" + string.Join("','", names) + "'";
  55.  
  56. string[] splitList = list.Split(',');
  57. string newList = "'" + string.Join("','", splitList) + "'";
  58.  
  59. var s = "Fred,Sam,Mike,Sarah";
  60. alert(s.replace(/b/g, "'"));
  61.  
  62. string list = "Fred,Sam,Mike,Sarah";
  63.  
  64. string[] splitList = list.Split(',');
  65.  
  66. for (int i = 0; i < splitList.Length; i++)
  67. splitList[i] = String.Format("'{0}'", splitList[i]);
  68.  
  69. string newList = String.Join(",", splitList);
  70.  
  71. Regex regex = new Regex(
  72. @"b",
  73. RegexOptions.ECMAScript
  74. | RegexOptions.Compiled
  75. );
  76.  
  77. string list = "Fred,Sam,Mike,Sarah";
  78. string newList = regex.Replace(list,"'");
  79.  
  80. string list = "Fred,Sam,Mike,Sarah";
  81. StringBuilder sb = new StringBuilder();
  82.  
  83. string[] listArray = list.Split(new char[] { ',' });
  84.  
  85. for (int i = 0; i < listArray.Length; i++)
  86. {
  87. sb.Append("'").Append(listArray[i]).Append("'");
  88. if (i != (listArray.Length - 1))
  89. sb.Append(",");
  90. }
  91. string newList = sb.ToString();
  92. Console.WriteLine(newList);
Add Comment
Please, Sign In to add comment