Serialize a string[] into a string with ";" as a separator - CSharp System

CSharp examples for System:String Split

Description

Serialize a string[] into a string with ";" as a separator

Demo Code



public class Main{
        /// <summary>
        /// Serialize a string[] into a string with ";" as a separator
        /// </summary>
        /// <param name="array">String array</param>
        /// <returns>Serialized string</returns>
        public static string Serialize(this string[] array)
        {//from w ww.  jav a2 s. c o  m
            return Serialize(array, ';');
        }
        #region Public Methods

        /// <summary>
        /// Serialize an array T[] into a delimited string
        /// </summary>
        /// <typeparam name="T">Any type, uses .ToString()</typeparam>
        /// <param name="array">array of type T</param>
        /// <param name="separator">Separator</param>
        /// <returns>Serialized string</returns>
        public static string Serialize<T>(this T[] array, char separator)
        {
            string str = string.Empty;
            for (int i = 0; i < array.Length; i++)
            {
                if (i == 0)
                    str += array[i];
                else
                    str += separator + array[i].ToString();
            }
            return str;
        }
}

Related Tutorials