My StringArrayHelper class

Previously I’ve posted a few Helper Classes . This post describes my StringArrayHelper class:

    using Iesi.Collections;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Xml;
    using System.Text;
 
    public static class StringArrayHelper
    {
        //created from CollectionsHelper.cs
        // Methods
            //’ignore new string, if already exist in the array
            // empty string considered as separate entry
            // function is case sensitive
            public static string[] AddToArray(string[] arrStrings, string NewString)
        {
                  if (null==arrStrings)
                  {
                        arrStrings=new string[0];
                  }
            if (Array.IndexOf(arrStrings, NewString) < 0)
            {
                ArrayList list1 = new ArrayList(arrStrings);
                list1.Add(NewString);
                return (string[]) list1.ToArray(Type.GetType(“System.String”));
            }
            return arrStrings;
        }
        // empty string considered as separate entry
        // function is case sensitive
        public static bool Replace(string[] arrStrings, string OldString,string NewString)//,bool bCaseSensitive)
        {
            if(arrStrings==null){   throw new ArgumentNullException(“arrStrings”);}
            int nPos = Array.IndexOf(arrStrings, OldString);
            if (nPos >= 0)
            {
                arrStrings[nPos] = NewString;
                return true;
            }
            return false;
        }
 
 
//          ‘May be useful? http://www.peterblum.com/CollectionsLibrary/AutoSortArrayList.aspx
//          ‘ignore duplicate strings
            public static string[] MergeArrays(string[] arrStrings, string[] NewArray)
        {
            ArrayList list1;
            if (arrStrings == null)
            {
                list1 = new ArrayList();
            }
            else
            {
                list1 = new ArrayList(arrStrings);
            }
            if(NewArray!=null)
            {
                list1.AddRange(NewArray);
                //        ‘Use Set to Remove Duplicates in an ArrayList http://www.devx.com/tips/Tip/20864
                // Iesi.Collections from http://www.codeproject.com/csharp/sets.asp#xx1367973xx ‘
                // alternatively use http://www.itu.dk/research/c5/ or http://www.codeproject.com/csharp/GenericISet.asp
                HybridSet set1 = new HybridSet(list1);
                list1.Clear();
                list1.AddRange(set1);
            }
            return (string[]) list1.ToArray(Type.GetType(“System.String”));
        }
 
        public static string[] RemoveEmptyLines(string[] arrStrings)
        {
            ArrayList list1 = new ArrayList(arrStrings);
            IEnumerator enumerator1 = arrStrings.GetEnumerator();
            while (enumerator1.MoveNext())
            {
                if (DataHelper.IsDBNullOrEmpty(RuntimeHelpers.GetObjectValue(enumerator1.Current)))
                {
                    list1.Remove(RuntimeHelpers.GetObjectValue(enumerator1.Current));
                }
            }
            return (string[]) list1.ToArray(Type.GetType(“System.String”));
        }
        public static string[] Trim (string[] arrStrings)
        {
            // http://blogs.msdn.com/marcod/archive/2005/11/01/GenericForEach.aspx
         //doesn’t work   Array.ForEach<string>(arrStrings, new Action<string>(delegate(string s)   { s = s.Trim(); }));
            for (int i = 0; i < arrStrings.Length; i++)
            {
                arrStrings[i] = arrStrings[i].Trim();
            }
            return arrStrings;
        }
        //24/10/2006 moved from StringHelper
        public static string[] ToLower(string[] Strings)
        {
            if (Strings != null)
            {
                for (int i = 0; i < Strings.Length; i++)
                {
                    Strings[i] = Strings[i].ToLower();
                }
            }
            return Strings;
        }
        public static string ConvertToQuotedCommaSeparatedValues(string[] asVal)
        {
            return ConvertToQuotedCommaSeparatedValues(asVal, true);
        }
        public static string ConvertToQuotedCommaSeparatedValues(string[] asVal,bool bIgnoreNullOrEmpty)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in asVal)
            {
                if (!String.IsNullOrEmpty(s))
                {
                    sb.AppendFormat(“{0},”, DataHelper.Quoted(s.Trim()));
                }
                else
                {if(bIgnoreNullOrEmpty==false)
                    {
                     sb.AppendFormat(“”,”);
                    }
                }
            }
            string sRet = StringHelper.LeftBeforeLast(sb.ToString(), “,”);
            return sRet;
        }
 
    }//end of 
 


Advertisements