Home > Mobile >  Find common string in list of strings C#
Find common string in list of strings C#

Time:04-06

I have a list of strings:

   List<string> list = new <string> 
   { 
        "AAB AOC 321"
        "AABAOC-WEB_A" 
        "AABAOC-WEB_B"
   }

Now I want to extract from the list of strings largest common prefix, which would ignore the white spaces (and special characters like "_" etc.) and give the below result:

"AABAOC"

I have tried the below method to achieve the same:

 var samples = new[] { 
            "AAB AOC 321"
            "AABAOC-WEB_A" 
            "AABAOC-WEB_B" };
    
    var commonPrefix = new string(samples.First().Substring(0, samples.Min(s => s.Length))
                    .TakeWhile((c, i) => samples.All(s => s[i] == c)).ToArray()); 

But the above method wont ignore white spaces and special characters and give the result "AAB", I have tried to play with the TakeWhile functionality but somehow not able to get what I want.

CodePudding user response:

You need to transform your strings - remove anything that should be ignored from them.

You can use Linq as well to do that:

using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var samples = new[] {"AAB AOC 321", "AABAOC-WEB_A", "AABAOC-WEB_B"};

        var ignored = new HashSet<char>("-& _");

        // use char[] internally
        // use ToList() to avoid multiple enumerations
        var transformed = samples
            .Select(s => s.Where(c => !ignored.Contains(c)).ToArray()).ToList();

        foreach (var s in transformed)
            System.Console.WriteLine(s);

        // just use transformed here - adjusted Substring to Take
        var commonPrefix = new string (transformed.First()
            .Take(transformed.Min(s => s.Length))
            .TakeWhile((c, i) => transformed.All(s => s[i] == c))
            .ToArray());

        System.Console.WriteLine($"\nPrefix {commonPrefix}");
    }
}

Output:

AABAOC321
AABAOCWEBA
AABAOCWEBB

Prefix AABAOC
  • Related