Question

I need a way to convert a strings collection into a unique string. This means that I need to have a different string if any of the strings inside the collection has changed.

I'm working on a big solution so I may wont be able to work with some better ideas. The required unique string will be used to compare the 2 collections, so different strings means different collections. I cannot compare the strings inside one by one because the order may change plus the solution is already built to return result based on 2 strings comparison. This is an add-on. The generated string will be passed as parameter for this comparison.

Thank you!

Was it helpful?

Solution

These both work by deciding to use the separator character of ":" and also using an escape character to make it clear when we mean something else by the separator character. We therefore just need to escape all our strings before concatenating them with our separator in between. This gives us unique strings for every collection. All we need to do if we want to make collections the same regardless or order is to sort our collection before we do anything. I should add that my sample uses LINQ and thus assumes the collection implements IEnumerable<string> and that you have a using declaration for System.LINQ

You can wrap that up in a function as follows

string GetUniqueString(IEnumerable<string> Collection, bool OrderMatters = true, string Escape = "/", string Separator = ":")
{
    if(Escape == Separator)
        throw new Exception("Escape character should never equal separator character because it fails in the case of empty strings");
    if(!OrderMatters) 
        Collection = Collection.OrderBy(v=>v);//Sorting fixes ordering issues.
    return Collection
        .Select(v=>v.Replace(Escape, Escape + Escape).Replace(Separator,Escape + Separator))//Escape String
        .Aggregate((a,b)=>a+Separator+b);
}

OTHER TIPS

What about using a hash function?

Considering you constraints, use a delimited approach:

pick a delimiter and an escape method. e.g. use ; and escape it bwithin strings y \;, also escape \ by \\

So this list of strings...

"A;bc"
"D\ef;"

...becomes "A\;bc;D\\ef\;"

It ain't pretty, but considering that it has to be a string, then the good old ways of csv and its brethren isn't all too bad.

By a "collection string" you mean "collection of strings"?

Here's a naive (but working) approach: sort the collection (to eliminate dependency on order), concat them, and take a hash of that (MD5 for instance).

Trivial to implement, but not very clever performance-wise.

Are you saying that you need to encode a string collection as a string. So for example the collection {"abc", "def"} may be encoded as "sDFSDFSDFSD" but {"a", "b"} might be encoded as "SDFeg". If so and you don't care about unique keys then you could use something like SHA or MD5.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top