Josh
Josh

Reputation: 1901

Removing duplicate substrings within a string in C#

How can I remove duplicate substrings within a string? so for instance if I have a string like smith:rodgers:someone:smith:white then how can I get a new string that has the extra smith removed like smith:rodgers:someone:white. Also I'd like to keep the colons even though they are duplicated.

many thanks

Upvotes: 7

Views: 6887

Answers (4)

Vivek Athalye
Vivek Athalye

Reputation: 2979

not sure why you want to keep the duplicate colons. if you are expecting the output to be "smith:rodgers:someone::white" try this code:

    public static string RemoveDuplicates(string input)
    {
        string output = string.Empty;
        System.Collections.Specialized.StringCollection unique = new System.Collections.Specialized.StringCollection();
        string[] parts = input.Split(':');
        foreach (string part in parts)
        {
            output += ":";
            if (!unique.Contains(part))
            {
                unique.Add(part);
                output += part;
            }
        }
        output = output.Substring(1);
        return output;
    }

ofcourse i've not checked for null input, but i'm sure u'll do it ;)

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292415

string input = "smith:rodgers:someone:smith:white";
string output = string.Join(":", input.Split(':').Distinct().ToArray());

Of course this code assumes that you're only looking for duplicate "field" values. That won't remove "smithsmith" in the following string:

"smith:rodgers:someone:smithsmith:white"

It would be possible to write an algorithm to do that, but quite difficult to make it efficient...

Upvotes: 23

CalebD
CalebD

Reputation: 5022

Something like this:

string withoutDuplicates = String.Join(":", myString.Split(':').Distinct().ToArray());

Upvotes: 8

Rob
Rob

Reputation: 45771

Assuming the format of that string:

var theString = "smith:rodgers:someone:smith:white";
var subStrings = theString.Split(new char[] { ':' });
var uniqueEntries = new List<string>();

foreach(var item in subStrings)
{
    if (!uniqueEntries.Contains(item))
    {
        uniqueEntries.Add(item);
    }
}

var uniquifiedStringBuilder = new StringBuilder();
foreach(var item in uniqueEntries)
{
    uniquifiedStringBuilder.AppendFormat("{0}:", item);
}

var uniqueString = uniquifiedStringBuilder.ToString().Substring(0, uniquifiedStringBuilder.Length - 1);

Is rather long-winded but shows the process to get from one to the other.

Upvotes: 2

Related Questions