Helper class for String and List

Helper class that consolidates most commonly used string and list conversions

Converting a ListToString:

 

private List<string> _multipleValues;

 

//Retreives the values of the list separated by the delimiter.

Methods.ListToString(_multipleValues, "*"));

 

 

//Converts Lists to string by separating separator and encloses with given string.

Methods.ListToString(multipleValues, ",", "'")));

 

Similarly if you need to reverse it , i.e. String to List, use The StringToStringList method.

 

  

 

using System;

using System.IO;

using System.Text;

using System.Reflection;

using System.Collections.Generic;

 

namespace Utilities

{

    /// <summary>

    /// UtilityMethods

    /// </summary>

    public sealed class Methods

    {

        /// <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>

        /// Converts Lists to string by separating separator and

        /// encloses with given string.

        /// </summary>

        /// <param name="list">The list.</param>

        /// <param name="separator">The separator.</param>

        /// <param name="encloser">The encloser.</param>

        /// <returns></returns>

        public static string ListToString(List<string> list, string separator, string encloser)

        {

            StringBuilder sb = new StringBuilder();

            foreach (string s in list)

            {

                sb.Append(string.Format("{0}{1}{0}{2}", encloser, 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>

        /// Lists to string.

        /// </summary>

        /// <param name="list">The list.</param>

        /// <param name="separator">The separator.</param>

        /// <returns></returns>

        public static string ListToString(List<int> list, string separator)

        {

            StringBuilder sb = new StringBuilder();

            foreach (int val in list)

            {

                sb.Append(string.Format("{0}{1}", val, 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>

        /// Converts String to int list.

        /// </summary>

        /// <param name="items">The items.</param>

        /// <param name="separator">The separator.</param>

        /// <returns></returns>

        public static List<int> StringToIntList(string items, char separator)

        {

            List<int> list = new List<int>();

            string []listItmes = items.Split(separator);

            foreach (string item in listItmes)

            {

                list.Add(int.Parse(item));

            }

            if (list.Count>0)

                return list;

            else

                return null;

        }

 

        /// <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  (1739 Views)