简体   繁体   English

检查列表中是否存在特定的字符串或字符串部分<string>

[英]Check if a particular string or string part exists in a List<string>

I have a function where I pass string value that should return me one string if that string or part of string (first 6 characters) exists in a list, if string or string part (first 6 characters) is not exists in list it should return null.我有一个函数,我传递字符串值,如果该字符串或字符串的一部分(前 6 个字符)存在于列表中,则应该返回一个字符串,如果列表中不存在字符串或字符串部分(前 6 个字符),则它应该返回空值。

For example if search for:例如,如果搜索:

EURUSD- it returns EURUSDf // good since EURUSD exists in list 
EURUSD  it returns EURUSDf // good since EURUSD exists in list
EURUSDf it returns EURUSDf // good since EURUSD exists in list
EURUSDu it returns EURUSDf // fail
EXCEUR is returns null     // good since EXCEUR is not exists in list

But what should it return for passed string is:但是它应该为传递的字符串返回什么:

EURUSD- should return EURUSDf or EURUSDu // since EURUSD exists in list
EURUSD  should return EURUSDf or EURUSDu // since EURUSD exists in list
EURUSDf should return EURUSDf // since EURUSDf exists in list
EURUSDu should return EURUSDu // since EURUSDu exists in list
EXCEUR should return null     // since EXCEUR is not exists in list

Problem is probably because foreach loop goes through each line and it stops on first match it find.问题可能是因为 foreach 循环遍历每一行并在找到的第一个匹配项时停止。 If you check in list it contains "EURUSDf", "EURUSDu" EURUSDf is always first so it will take him, it is ok on some cases but it is not if i search for EURUSDu strict and this returns me EURUSDf .如果您签入列表,它包含"EURUSDf", "EURUSDu" EURUSDf 始终是第一个,所以它会带他,在某些情况下是可以的,但如果我搜索EURUSDu strict 并且这返回我EURUSDf

Since "EURUSDf", "EURUSDu" exists in list and i search for EURUSDu how to skip to it and return EURUSDu or if i search for EURUSDf to return EURUSDf ?由于"EURUSDf", "EURUSDu"存在于列表中,我搜索EURUSDu如何跳到它并返回EURUSDu或者如果我搜索EURUSDf以返回EURUSDf

Here is code这是代码

string symbol = MapSymbol("EURUSDu"); // pass string and get output
Console.WriteLine($"Symbol is {symbol}"); // output

public static string MapSymbol(string symbol)
{
    try
    {
        symbol = symbol.Trim();
        string symbolReturn = "";

        List<string> symbolList = new List<string>() { "AUDCADu", "AUDJPYu", "AUDNZDu", "AUDUSDf", "AUDUSDu", "CADJPYu", "EURAUDu", "EURCADu", "EURCHFu", "EURGBPu", "EURJPYu", "EURNZDu", "EURUSDf", "EURUSDu", "GBPAUDu", "GBPCADu", "GBPCHFu", "GBPJPYu", "GBPNZDu", "GBPUSDf", "GBPUSDu", "GER30u", "HK50u", "JPN225u", "NAS100u", "NZDCADu", "NZDUSDf", "NZDUSDu", "SPX500u", "UK100u", "UKOILu", "US30u", "USDCADf", "USDCADu", "USDCHFf", "USDCHFu", "USDJPYf", "USDJPYu", "USDXu", "USOILu", "XAGUSDf", "XAGUSDfv", "XAGUSDu", "XAUUSDf", "XAUUSDfv", "XAUUSDu", "XINA50u" };

        foreach (var item in symbolList)
        {
            if (item == symbol)
            {
                symbolReturn = item; break;
            }
            else if (item == symbol.Substring(0, Math.Min(symbol.Length, 6)))
            {
                symbolReturn = item; break;
            }
            else if (item.Substring(0, Math.Min(item.Length, 6)) == symbol)
            {
                symbolReturn = item; break;
            }
            else if (item.Substring(0, Math.Min(item.Length, 6)) == symbol.Substring(0, Math.Min(symbol.Length, 6)))
            {
                symbolReturn = item; break;
            }
            else
            {
                symbolReturn = null;
            }
        }
        return symbolReturn;
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception in maping symbol, {ex.Message}");
        return null;
    }
}

You could try such method:你可以试试这样的方法:

static List<string> symbolList = new List<string>() { ... };

public static string MapSymbol(string symbol)
{
  // we have exact match
  if (symbolList.Contains(symbol)) return symbol;
  // look for first 6 character match
  var sixCharPrefix = symbol.Substring(0, 6);
  // returns first matched string, if not found return default for string, which is null
  return symbolList.FirstOrDefault(s => s.StartsWith(sixCharPrefix));
}

If you don't want to traverse the list multiple times or use linq, then you could introduce the concept of priority:如果不想多次遍历列表或使用linq,那么可以引入优先级的概念:

public static string MapSymbol(string symbol)
{
    try
    {
        symbol = symbol.Trim();
        string symbolReturn = null;
        int priority = 5;

        List<string> symbolList = new List<string>() { "AUDCADu", "AUDJPYu", "AUDNZDu", "AUDUSDf", "AUDUSDu", "CADJPYu", "EURAUDu", "EURCADu", "EURCHFu", "EURGBPu", "EURJPYu", "EURNZDu", "EURUSDf", "EURUSDu", "GBPAUDu", "GBPCADu", "GBPCHFu", "GBPJPYu", "GBPNZDu", "GBPUSDf", "GBPUSDu", "GER30u", "HK50u", "JPN225u", "NAS100u", "NZDCADu", "NZDUSDf", "NZDUSDu", "SPX500u", "UK100u", "UKOILu", "US30u", "USDCADf", "USDCADu", "USDCHFf", "USDCHFu", "USDJPYf", "USDJPYu", "USDXu", "USOILu", "XAGUSDf", "XAGUSDfv", "XAGUSDu", "XAUUSDf", "XAUUSDfv", "XAUUSDu", "XINA50u" };

        foreach (var item in symbolList)
        {
            if (item == symbol)
            {
                symbolReturn = item; 
                break; // best priority so no need to look at the rest of the list
            }
            else if (priority > 2 && item == symbol.Substring(0, Math.Min(symbol.Length, 6)))
            {
                priority = 2;
                symbolReturn = item;
            }
            else if (priority > 3 && item.Substring(0, Math.Min(item.Length, 6)) == symbol)
            {
                priority = 3;
                symbolReturn = item;
            }
            else if (priority > 4 && item.Substring(0, Math.Min(item.Length, 6)) == symbol.Substring(0, Math.Min(symbol.Length, 6)))
            {
                priority = 4;
                symbolReturn = item;
            }
        }

        return symbolReturn;
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception in maping symbol, {ex.Message}");
        return null;
    }
}

One solution I can offer is that you extract your tests in a separate collection using the Func data type, the position will be the priority here.我可以提供的一种解决方案是,您使用Func数据类型在单独的集合中提取测试,这里的位置将是优先事项。 Then you go test rule by test rule and iterate through your collection.然后你通过测试规则测试规则并迭代你的集合。 If the first rule gives you a positive result, then you stop looking.如果第一条规则给你一个积极的结果,那么你就停止寻找。 If not, then you iterate again using the next rule and so on.如果不是,则使用下一条规则再次迭代,依此类推。 I hope you get the drift.我希望你能明白。

public static string MapSymbol(string symbol)
{
    try
    {
        symbol = symbol.Trim();
        string symbolReturn = "";

        List<string> symbolList = new List<string>() { "AUDCADu", "AUDJPYu", "AUDNZDu", "AUDUSDf", "AUDUSDu", "CADJPYu", "EURAUDu", "EURCADu", "EURCHFu", "EURGBPu", "EURJPYu", "EURNZDu", "EURUSDf", "EURUSDu", "GBPAUDu", "GBPCADu", "GBPCHFu", "GBPJPYu", "GBPNZDu", "GBPUSDf", "GBPUSDu", "GER30u", "HK50u", "JPN225u", "NAS100u", "NZDCADu", "NZDUSDf", "NZDUSDu", "SPX500u", "UK100u", "UKOILu", "US30u", "USDCADf", "USDCADu", "USDCHFf", "USDCHFu", "USDJPYf", "USDJPYu", "USDXu", "USOILu", "XAGUSDf", "XAGUSDfv", "XAGUSDu", "XAUUSDf", "XAUUSDfv", "XAUUSDu", "XINA50u" };

        // functions can be saved as Func<string, string, bool>
        // that means it takes 2 strings as input parameters and returns a bool
        List<Func<string, string, bool>> TestCases = new List<System.Func<string, string, bool>>()
        {
            (item, sym) => item == sym,
            (item, sym) => item == sym.Substring(0, Math.Min(sym.Length, 6)),
            (item, sym) => item.Substring(0, Math.Min(item.Length, 6)) == sym.Substring(0, Math.Min(sym.Length, 6))
        };

        bool matchFound = false;
        // iteration over each test rules
        foreach (var test in TestCases)
        {
            // iteration ver your collection
            foreach (var item in symbolList)
            {
                // test for a match
                if(test(item, symbol))
                {
                    matchFound = true;
                    symbolReturn = item;
                }
            }

            if (matchFound)
            {
                break;
            }
        }

        return matchFound ? symbolReturn : null;
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Exception in maping symbol, {ex.Message}");
        return null;
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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