Converting a List to string and vice-versa

Helper methods to convert a List to String and Vice-Versa

/// <summary>
/// Converts List to string with given separator.
/// </summary>
/// <param name="list">The list.</param>
/// <param name="separator">The separator.</param>
/// <returns></returns>
public static string ListToString(List<string> list,string separator)
{
     StringBuilder sb = new StringBuilder();



foreach (string s in list) { sb.Append(string.Format("{0}{1}", s, separator));

}









string returnString = string.Empty;
//Remove the last separator from the list if (sb.Length > 0) { returnString = sb.Remove(sb.ToString().LastIndexOf(separator), separator.Length).ToString(); }
return returnString; } /// <summary> /// Strings to string list. /// </summary> /// <param name="items">The items.</param> /// <param name="separator">The separator.</param> /// <returns></returns> public static List<string> StringToStringList(string items, char separator) { List<string> list = new List<string>(); string[] listItmes = items.Split(separator);
foreach (string item in listItmes) { list.Add(item); }
if (list.Count > 0) return list;
else

return null; }
By [)ia6l0 iii   Popularity  (1803 Views)