【问题标题】:Remove all the occurences that contain a part of the initial string删除包含部分初始字符串的所有出现
【发布时间】:2022-01-19 11:45:38
【问题描述】:

我有一个字符串列表,其中包含例如以下项目:

0$1, 0$2, 0$5, 1$1, 1$8

如果已经存在的话,我的意愿是用 1 美元移除其他项目。 在该示例中,预期输出为:

0$1, 0$2, 0$5, 1$8

有没有更简洁的方法来实现这一点,使用 LINQ 或其他任何东西,而不是做这样的事情?

For Each str As String In ddlElecDBFilter.SelectedItemsList
    Dim currentString As String = str
    'DO A FOR EACH LOOP on the other elements (starting from the current index + 1 and delete them 

Next

提前谢谢你

【问题讨论】:

  • 您的问题被标记为 C#,但您的代码使用不同的语言(看起来像 VB?)。你(想)使用什么语言?
  • 是的,抱歉,我添加了 VB.NET。但如果有人想出 C# 解决方案,翻译起来很容易
  • 创建一个新列表,并从您的字符串列表中添加项目(如果它们尚未在新列表中)。列表的大致大小是多少 - 数十个、数百个或更多字符串?
  • @AndrewMorton 我会说列表的大小最大为十。这可能是最好的解决方案,谢谢。我可以尝试做这样的事情
  • @solrin 实际上,使用 Dictionary(Of String, String) 其中第一个字符串是必须唯一的部分可能更容易。 Dictionary.ContainsKey 的文档有一个示例,几乎可以满足您的需求。

标签: c# vb.net linq


【解决方案1】:

不确定是否有“更清洁”的方式来做到这一点:

Dim input As String = "0$1, 0$2, 0$5, 1$1, 1$8"

Dim uniqueValues As New List(Of String)
input.Split(", ".ToCharArray, StringSplitOptions.RemoveEmptyEntries) _
    .Where(Function(x) x.Contains("$")) _
    .Select(Function(y) New KeyValuePair(Of String, String)(y.Substring(y.IndexOf("$")), y)) _
    .ToList() _
    .ForEach(Sub(pair)
                 If Not uniqueValues.Any(Function(x) x.EndsWith(pair.Key)) Then
                     uniqueValues.Add(pair.Value)
                 End If
             End Sub)
Dim output As String = String.Join(", ", uniqueValues)

【讨论】:

    【解决方案2】:

    既然你说 C# 是可以接受的,那么我的答案是 C#。如果您在翻译到 VB.Net 时遇到问题,我可以添加。

    我不会说 LINQ 更干净,但它很短。当您需要累积物品时,Aggregate 是使用的方法。当您需要在处理时保持某种类型的状态时,AggregateValueTuple(或其他容器)允许您在处理时保留值。

    如果ss 包含您的List<string>,那么ans 将包含处理后的字符串:

    var ans = ss.Select(s => s.Split(", ")
                .Aggregate((ans: Enumerable.Empty<string>(), seen: false), // accumulate result in ans, track whether $1 has been seen
                           (ans_seen, item) => ans_seen.seen
                                                ? (item.EndsWith("$1") ? ans_seen : (ans_seen.ans.Append(item), ans_seen.seen))
                                                : (ans_seen.ans.Append(item), item.EndsWith("$1"))
                          )
                .ans.Join(", ")) // string extension method that encapsulates String.Join
                .ToList();
    

    这里使用了明显的字符串扩展方法来封装String.Join

    我会说最好创建一个使用循环的函数,并且只使用 LINQ 进行List 处理:

    public static class StringExt {
        public static string KeepOnlyFirstDollarOne(this string s) {
            var seen = false;
            var resultList = new List<string>();
            foreach (var item in s.Split(", ")) {
                if (seen) {
                    if (!item.EndsWith("$1"))
                        resultList.Add(item);
                }
                else {
                    resultList.Add(item);
                    seen = item.EndsWith("$1");
                }
            }
            return String.Join(", ", resultList);
        }
    }
    

    那么得到答案就这么简单:

    var ans = ss.Select(s => s.KeepOnlyFirstDollarOne()).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 2022-11-03
      • 2012-12-06
      • 1970-01-01
      相关资源
      最近更新 更多