devxlogo

Converting Between List and String Types

Converting Between List and String Types

These handy functions let you easily convert from a generic string List to a delimited string, or from a delimited string to a string List:

/// /// Converts List to string with given separator./// /// The list./// The separator./// public static string ListToString(   List 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;}/// /// Strings to string list./// /// The items./// The separator./// public static List StringToStringList(string items, char separator){      List list = new List();      string[] listItmes = items.Split(separator);      foreach (string item in listItmes)      {          list.Add(item);      }      if (list.Count > 0)          return list;      else          return null;}
See also  lenso.ai - an AI reverse image search platform
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist