Please find below for more details.
I have a text with me ""1200005 # 28102016_0612 Inv.ACHExtractDetail POC.DiscountDetail Scanner Test1" & also a list of keyword
keywords.Add(new KeywordCTIBO() { Keyword = "Scanner" });
keywords.Add(new KeywordCTIBO() { Keyword = "Inv.ACHExtractDetail" });
keywords.Add(new KeywordCTIBO() { Keyword = "POC.DiscountDetail" });
What i require is i need to extract "Inv.ACHExtractDetail" from text since it is present in the list. What i mean is the first occurrence of keyword from text if and only if it exist in the list.
Below code works fine but i am looking much better if possible without using loops.
List<KeywordCTIBO> keywords = new List<KeywordCTIBO>();
KeywordCTIBO matchedKeyword = null;
keywords.Add(new KeywordCTIBO() { Keyword = "Scanner" });
keywords.Add(new KeywordCTIBO() { Keyword = "Inv.ACHExtractDetail" });
keywords.Add(new KeywordCTIBO() { Keyword = "POC.DiscountDetail" });
string text = "1200005 # 28102016_0612 Inv.ACHExtractDetail POC.DiscountDetail Scanner Test1";
string [] arr = text.ToLower().Split(',', ' ', ';');
string s = string.Empty;
foreach(var r in arr){
var matchedKeyWords = keywords.Where(f => r.Contains(f.Keyword.ToLower())).Select(p => new { KeyWordBO = p, Index = text.ToLower().IndexOf(p.Keyword.ToLower()) });
foreach (var f in matchedKeyWords)
{
s = f.KeyWordBO.Keyword;
}
}
}
public class KeywordCTIBO
{
public string Keyword { get; set; }
}
What I have tried:
List<KeywordCTIBO> keywords = new List<KeywordCTIBO>();
KeywordCTIBO matchedKeyword = null;
keywords.Add(new KeywordCTIBO() { Keyword = "Scanner" });
keywords.Add(new KeywordCTIBO() { Keyword = "Inv.ACHExtractDetail" });
keywords.Add(new KeywordCTIBO() { Keyword = "POC.DiscountDetail" });
string text = "1200005 # 28102016_0612 Inv.ACHExtractDetail POC.DiscountDetail Scanner Test1";
string [] arr = text.ToLower().Split(',', ' ', ';');
string s = string.Empty;
foreach(var r in arr){
var matchedKeyWords = keywords.Where(f => r.Contains(f.Keyword.ToLower())).Select(p => new { KeyWordBO = p, Index = text.ToLower().IndexOf(p.Keyword.ToLower()) });
foreach (var f in matchedKeyWords)
{
s = f.KeyWordBO.Keyword;
}
}
}
public class KeywordCTIBO
{
public string Keyword { get; set; }
}
}