简体   繁体   中英

Check the value exists in List using C#

I have List as mentioned below. Now When I am going to add new string value into this list, My method GetNewCopiedValue has to check the new text value into the list lstNames .If the name does not exist in the list it should return the value as it is. If the new string value is exist already in the list, it has to return the string with respective index number as like EC(1).For example, If I am sending EC(1) again to the list, the method has to check the value in the list and It should return EC(3) since EC(1) is exist already in the list, the method has to check the similar values and it should return the value with next index number which is not there in the list.

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

Actual Input: EC(2)

Expected output: EC(3)

I have tried with some logic but it was not working with all input strings. It worked for few cases only. Please help me on this.

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

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