簡體   English   中英

使用 C# 檢查 List 中的值是否存在

[英]Check the value exists in List using C#

我有下面提到的列表。 現在,當我要將新的字符串值添加到此列表中時,我的方法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;
}

實際輸入:EC(2)

預期輸出:EC(3)

我嘗試了一些邏輯,但它不適用於所有輸入字符串。 它僅適用於少數情況。 請幫我解決這個問題。

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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM