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
My Requirements:
Terminate with carriage-return and line-feed.
string.Join(",", lCol.Select(s => s == null ? null : ("\"" + s.Replace("\"", "\"\"") + "\""))) + "\r\n";
I can't write C# code, but this simple JavaScript code is probably easy to adapt:
var s = "Fred,Sam,Mike,Sarah";
alert(s.replace(/\b/g, "'"));
It just replace bounds (start/end of string, transition from word chars non punctuation) by single quote.
If you are using JSON, following function would help
var string[] keys = list.Split(',');
console.log(JSON.stringify(keys));
I have found a new solution for this problem
I bind a list by selected items values from the grid using linq, after that added a comma delimited string for each string collections by using String.Join() properties.
String str1 = String.Empty;
String str2 = String.Empty;
//str1 = String.Join(",", values); if you use this method,result "X,Y,Z"
str1 =String.Join("'" + "," + "'", values);
//The result of str1 is "X','Y','Z"
str2 = str1.Insert(0, "'").Insert(str1.Length+1, "'");
//The result of str2 is 'X','Y','Z'
I hope this will helpful !!!!!!
My "less sophisticated" approach ... I suppose it's always good practice to use a StringBuilder because the list can be very large.
string list = "Fred,Sam,Mike,Sarah";
StringBuilder sb = new StringBuilder();
string[] listArray = list.Split(new char[] { ',' });
for (int i = 0; i < listArray.Length; i++)
{
sb.Append("'").Append(listArray[i]).Append("'");
if (i != (listArray.Length - 1))
sb.Append(",");
}
string newList = sb.ToString();
Console.WriteLine(newList);
string[] splitList = list.Split(',');
string newList = "'" + string.Join("','", splitList) + "'";