IntroductionI have read in forums there are many users asking questions about removing duplicates words from the string; remove duplicate elements from the ArrayList. I have already post a snippets for remove the duplicate elements from ArrayList using C# . So this snippet shows how to remove the repeating words in string using c#. ImplementationThere are many ways to do this, but I’m using Dictionary object to keep non duplicate words as key and parse string in loop and add into another string. Finally return duplicate words removed string. C# Code using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class StringUtility
{
public static string RemoveRepeatWords(string source)
{
Dictionary<string, bool> listofUniqueWords = new Dictionary<string, bool>();
StringBuilder destBuilder = new StringBuilder();
string[] spilltedwords = source.Split(new char[] { ' ', ',', ';', '.' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in spilltedwords)
{
string lower = item.ToLower();
if (!listofUniqueWords.ContainsKey(lower))
{
destBuilder.Append(item).Append(' ');
listofUniqueWords.Add(lower, true);
}
}
return destBuilder.ToString().Trim();
}
}
}
How to use ?using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string source = "This is test string composed by RRaveen demostration purpose. if this string has duplicate words this method will remove and reprint.";
Console.WriteLine("{0} :{1}", "source", source);
Console.WriteLine("{0} :{1}", "Destionation", StringUtility.RemoveRepeatWords(source));
Console.ReadLine();
}
}
}
Output source :This is test string composed by RRaveen demostration purpose. if this string has duplicate words this method will remove and reprint.
Destionation :This is test string composed by RRaveen demostration purpose if has duplicate words method will remove and reprint hopes help.thank you for reading. |