[英]Check if a particular string or string part exists in a List<string>
我有一個函數,我傳遞字符串值,如果該字符串或字符串的一部分(前 6 個字符)存在於列表中,則應該返回一個字符串,如果列表中不存在字符串或字符串部分(前 6 個字符),則它應該返回空值。
例如,如果搜索:
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
但是它應該為傳遞的字符串返回什么:
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
問題可能是因為 foreach 循環遍歷每一行並在找到的第一個匹配項時停止。 如果您簽入列表,它包含"EURUSDf", "EURUSDu"
EURUSDf 始終是第一個,所以它會帶他,在某些情況下是可以的,但如果我搜索EURUSDu
strict 並且這返回我EURUSDf
。
由於"EURUSDf", "EURUSDu"
存在於列表中,我搜索EURUSDu
如何跳到它並返回EURUSDu
或者如果我搜索EURUSDf
以返回EURUSDf
?
這是代碼
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;
}
}
你可以試試這樣的方法:
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));
}
如果不想多次遍歷列表或使用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;
}
}
我可以提供的一種解決方案是,您使用Func數據類型在單獨的集合中提取測試,這里的位置將是優先事項。 然后你通過測試規則測試規則並迭代你的集合。 如果第一條規則給你一個積極的結果,那么你就停止尋找。 如果不是,則使用下一條規則再次迭代,依此類推。 我希望你能明白。
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.