Filtering common starting/ending characters from array/list of strings

Ok so for example I have an array of strings with each string as below:

364VMS1029 
364VMSH920 
364VMSH192 
364VMSU839 
364VMN2382 
364VMR223
364VMR2X3 
364VMN829 
364VMN8757 
364VMN831

How can I dynamically get the program to recognise the common characters among all strings in the array, which in this case is 364VM and filter them out?
If there’s no common character, then don’t do anything.

When you have a complicated problem that you can’t solve, try breaking it down into simpler problems and solve those.

Your problem rephrased:

Remove the common prefix and suffix from a list of strings.

A simpler problem would be:

Find the common prefix for a pair of strings.

This should be much simpler to solve, for example like this:

string GetPrefix(string first, string second)
{
    int prefixLength = 0;

    for (int i = 0; i < Math.Min(first.Length, second.Length); i++)
    {
        if (first[i] != second[i])
            break;

        prefixLength++;
    }

    return first.Substring(0, prefixLength);
}

Now that you have this, you can build back to the original problem:

Find the common prefix for a list of strings.

Here, it’s very helpful to realize that the prefix of three strings is the same as the prefix of the prefix of the first two strings and the third string. (Hmm, that sounds confusing, maybe it will be clearer in a more formal notation: prefix(A, B, C) = prefix(prefix(A, B), C).)

This means that you can use the LINQ method Aggregate() on the GetPrefix() method above to get the prefix of a whole list of strings:

string GetPrefix(IEnumerable<string> strings)
{
    return strings.Aggregate(GetPrefix);
}

The next step:

Remove the common prefix from a list of strings.

Now that we can find the common prefix, we can remove it using LINQ Select() and Substring():

IEnumerable<string> RemovePrefix(IEnumerable<string> strings)
{
    var prefix = GetPrefix(strings);

    return strings.Select(s => s.Substring(prefix.Length, s.Length - prefix.Length));
}

This assumes you want to get a new sequence containing the filtered strings. If you want to modify an existing collection, use a for loop instead of the Select().

One last step:

Remove the common prefix and suffix from a list of strings.

I’ll leave this as an exercise for the reader. This answer contains simple code for reversing a string, which could be helpful. (I think you don’t need the code from other answers to that question, since it looks like your strings are ASCII-only.)

You have to compare every character at the beginning of the strings to see how long the longest common prefix is, and stop once you’ve found a difference.

(Depending on how expensive string access is vs. sorting, you might save time by first sorting the list and then looking only at the first and last string – whatever they have in common will, by definition, also be common to all strings between them. But it is a matter of profiling to find out whether this saves or adds time.)

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật