【问题标题】:Check the value exists in List using C#使用 C# 检查 List 中是否存在值
【发布时间】:2021-01-10 01:26:45
【问题描述】:

我有如下所述的列表。现在,当我要将新字符串值添加到此列表中时,我的方法 GetNewCopiedValue 必须将新文本值检查到列表 lstNames 中。如果名称不存在于它应该按原样返回值的列表。如果列表中已经存在新的字符串值,则必须返回具有相应索引号的字符串,如 EC(1)。例如,如果我再次将 EC(1) 发送到列表,则该方法必须检查列表中的值,它应该返回 EC(3),因为 EC(1) 已经存在于列表中,该方法必须检查相似的值,它应该返回列表中不存在的下一个索引号的值.

Main()
{
    List<string> lstNames=new List<string>{"Ecard","EC","EC(1)","EC(2)","NCard(1)"};
    var copiedValue= GetNewCopiedValue(lstNames,EC(2));
    Console.WriteLine("Copied Text is :"+copiedValue);
}
public static string GetNewCopiedValue(List<string> lstNames,string sourceLabel)
{
  label = sourceLabel;         

        if (lstNames.Any(i => i.Equals(sourceLabel)))
        {
            var labelSubstring = sourceLabel.Substring(0, sourceLabel.Length - 2);
            var sameNameList = lstNames.Where(i => i.Contains(labelSubstring)).ToList();
            int count = sameNameList.Count+1;
            label = labelSubstring + count + ")";
            while (lstNames.Any(i => i.Equals(label)))
            {
                var indexLabel = sourceLabel.Substring(sourceLabel.Length-2,1);
                var maxIndex = sameNameList.Max(i =>int.Parse( i.Substring(sourceLabel.Length - 2, 1)));
                int labelCount = maxIndex + 1;
                label = labelSubstring + labelCount + ")";                   
            }
        }
        return label;
}

实际输入: 欧共体(2)

预期输出: 欧共体(3)

我尝试了一些逻辑,但它不适用于所有输入字符串。它仅适用于少数情况。 请帮我解决这个问题。

【问题讨论】:

  • 请包含一个minimal reproducible example...任何人都可以复制/粘贴到一个空的c#文件中并执行的东西。否则我们必须猜测未显示的代码可能在做什么。如果不确定它是否是minimal reproducible example 表单,请使用dotnetfiddle.net,如果它可以在那里运行,那就很好。
  • 如果您的列表为空并且您尝试插入a 四次,这些值应该是多少?
  • 还包括实际输入、预期输出和实际输出。这使我们能够理解所显示代码的行为以及预期的行为应该是什么。
  • @ZoharPeled:应该是这样的:a、a (1)、a(2)、a(3)
  • 当管理员甚至不给一个人在关闭问题之前改进问题的机会时,我一定会喜欢它。

标签: c# .net regex list linq


【解决方案1】:

https://dotnetfiddle.net/dFrzhA

    public static void Main() {
        List<string> lstNames= new List<string>{"Ecard","EC","EC(1)","EC(2)","NCard(1)"};
        var copiedValue= GetNewCopiedValue(lstNames, "EC(1)");
        Console.WriteLine("Copied Text is :" + copiedValue);
    }
    
    public static string GetNewCopiedValue(List<string> lstNames, string ValueToCopyInList) {
        string newName;

        if (!lstNames.Contains(ValueToCopyInList)) {
            newName = ValueToCopyInList;
        } else {
            int? suffix = ParseSuffix(ValueToCopyInList);
            string baseName = suffix == null ? ValueToCopyInList : ValueToCopyInList.Substring(0, ValueToCopyInList.LastIndexOf('('));
            suffix = suffix ?? 1;
            newName = baseName + "(" + suffix + ")";
            
            while (lstNames.Contains(newName)) {
                suffix++;
                newName = baseName + "(" + suffix + ")";
            }
        }
        
        lstNames.Add(newName);
        return newName;
    }
    
    public static int? ParseSuffix(string value) {
        int output;
        
        if (string.IsNullOrEmpty(value)) return null;
        if (!value.EndsWith(")")) {
            return null;
        }
        
        var idxStart = value.LastIndexOf('(');
        var strResult = value.Substring(idxStart + 1, value.Length - (idxStart + 2));
        
        if (int.TryParse(strResult, out output))
            return output;
        
        return null;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 2014-01-18
    • 1970-01-01
    • 2011-10-06
    • 2011-05-15
    相关资源
    最近更新 更多