简体   繁体   中英

Remove items from List<string> where substring is duplicated (C#)

Lets say I have a list

TEST1 10:05:45
TEST2 10:03:23
TEST3 10:01:28
TEST2 13:03:23
TEST3 16:01:28

I would like to filter my list and remove all lines where the name is duplicated, so my output would be this here:

TEST1 10:05:45

This is what I have tryed so far:

List<string> numberList = new List<string>() {
    "TEST1 10:05:45", "TEST2 10:03:23", "TEST3 10:01:28", 
    "TEST2 13:03:23", "TEST3 16:01:28" };
          
foreach (var x in numberList
    .GroupBy(i => i)
    .Where(g => g.Count() == 1)
    .Select(g => g.Key))
{
    MessageBox.Show(x.ToString());
}

Unfortunately this doesnt work since the Time Stamps are different altough the name is the same

Anyone got a clue what I can do here ?

Don't use the complete string as grouping key, but a substring that goes to the first space:

numberList
   .GroupBy(i => i.Substring(0, i.IndexOf(' ')))
   .Where(g => g.Count() == 1)
   .Select(g => g.First())

Online demo: https://dotnetfiddle.net/yiCbef

Your comments show a completely different input than shown in your question. The string you are trying to parse is a HTML string. Not a string with space separated items. Example (excerpt from longer text):

<div><span class="watchlist " data-wkn="A1CSR6" title="Zur..." />

You are searching for duplicates of the key data-wkn .

While it is generally not a good idea to parse HTML with regex, it might work with this simple scenario. The idea is to search for a text embedded between data-wkn=" and " . This can be achieved with the general regex pattern (?<=prefix)find(?=suffix) which matches an expression ( find ) between a prefix and a suffix .

string key = Regex.Match(input, "(?<=data-wkn=\").*?(?=\")").Value;

Note that we have to escape the double quotes with \\" in string literals in C#.

With

  • prefix = data-wkn=" , the string before the key (with " escaped as \\" ).
  • find = .*? any number of any character, but a few as possible.
  • suffix = " , the string after the key (with " escaped as \\" ).

Usage:

var filteredList = numberList
   .GroupBy(s => Regex.Match(s, "(?<=data-wkn=\").*?(?=\")").Value)
   .Where(g => g.Count() == 1)
   .Select(g => g.First());

See: Regular Expression Language - Quick Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM