Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I am using this method to remove empty entries, now I need to check for duplicates but I am on a very old version of VS 2005 theres no linq I can't use the duplicate method. Any help would be nice. Thank!

String[] hostName = htbclb.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
Posted
Comments
Sergey Alexandrovich Kryukov 22-Oct-12 0:44am    
First of all, this is not a question. What have you done so far? It's completely unclear how split is related to the question. First of all you need to think about is why do you have duplicate in first place. Maybe, you should have written the code not allowing to create duplicates; it would be easier and way more efficient.
--SA

1 solution

You can tweak the solution to make to suit your needs!

One possible solution is using the following snippet.
C#
public static class ListExtensions
   {
       /// <summary>
       /// Add
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="List"></param>
       /// <param name="item"></param>
       /// <returns></returns>
       public static bool Add<T>(this List<T> List, T item)
       {
           bool isAdded = false;

           // if this is not a duplicate item
           if (!List.Contains(item))
           {
               // Add this item in the list
               List.Add(item);
               isAdded = true;
           }
           return isAdded;
       }

       /// <summary>
       /// ToStringEx
       /// </summary>
       /// <param name="StringArray"></param>
       /// <returns></returns>
       public static string ToStringEx(this string[] StringArray)
       {
           StringBuilder stringBuilder = new StringBuilder();
           foreach (string stringItem in StringArray)
           {
               stringBuilder.AppendFormat("{0},", stringItem);
           }
           return stringBuilder != null ? stringBuilder.ToString().TrimEnd (new char[] {','}) : string.Empty;
       }
   }

   public class Program
   {
       static void Main()
       {
           List<string> stringList = null;
           string longString = null;
           string[] stringArray = null;
           try
           {
               stringList = new List<string>();
               longString = "BOOK,COOK,BOOK,COOK,BOOK,LOOK,COOK,COOK,BOOK";
               stringArray = longString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

               // Enumerate over the stringArray
               foreach (string stringItem in stringArray)
               {
                   // Add this item in the list
                   stringList.Add<string>(stringItem);
               }

               // Converting List<string> to string[]
               stringArray = stringList.ToArray();

               // Converting string[] to comma delimited string
               longString = stringArray.ToStringEx();
           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.StackTrace);
           }
           Console.ReadKey();
       }

Or you can stip the code from the extension methods and use inline -
C#
static void Main()
       {
           List<string> stringList = null;
           string longString = null;
           string[] stringArray = null;
           StringBuilder stringBuilder = null;
           try
           {
               stringList = new List<string>();
               longString = "BOOK,COOK,BOOK,COOK,BOOK,LOOK,COOK,COOK,BOOK";
               stringArray = longString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
               // Enumarate over the stringArray
               foreach (string stringItem in stringArray)
               {
                   // if this is not a duplicate item
                   if (!stringList.Contains(stringItem))
                   {
                       // Add this item in the list
                       stringList.Add(stringItem);
                   }
               }
               // Converting List<string> to string[]
               stringArray = stringList.ToArray();
               // Converting string[] to comma delimited string
               stringBuilder = new StringBuilder();
               foreach (string stringItem in stringArray)
               {
                   stringBuilder.AppendFormat("{0},", stringItem);
               }
               longString = stringBuilder.ToString();
           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.StackTrace);
           }
           Console.ReadKey();
       }

Regards,
ErSehmi
 
Share this answer
 
v3
Comments
Member 9473758 22-Oct-12 3:20am    
Thanks for taking time out and help! This is a final solution that I implemented I wanted it to be an array output.

string[] stringArray = null;
//StringBuilder stringbuilder = null;
stringList = new List<string>();
stringArray = htbclb.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
foreach (string stringItem in stringArray)
{
if (!stringList.Contains(stringItem))
{
stringList.Add(stringItem);
}

}
stringArray = stringList.ToArray();
StringBuilder SB = new StringBuilder();
foreach (string stringItem in stringArray)
{
SB.AppendFormat("{0},", stringItem);
}
String[] hostName = stringArray;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900