2012-12-11 36 views
1

的名单我有一个字符串的list其中有4款产品:查找字符串采取关键字,并期待它在字符串

Orange Lemon Pepper Tomato

另外,我有一个String str其中有一句:

Today, I ate a tomato and an orange.

1)如何检查str是否有一些来自list的关键字?不考虑大写或小写字母,基本上捕捉任何匹配的东西?

我试过这个,但它不起作用,因为它会查找相同的单词。 list.Contains(str)

Dim result As String() = list.FindAll(str, Function(s) s.ToLower().Contains(str))但也没有工作。

2)如果字tomatostrtomatoes,我怎么还检测tomato部分,并丢弃es一部分?

任何意见或建议?

回答

3
var list = new string[] { "Orange", "Lemon", "Pepper", "Tomato" }; 
var str = "Today, I ate a tomato and an orange."; 

随着LINQ和正则表达式,你可以检查是否字符串包含任何关键字:

list.Any(keyword => Regex.IsMatch(str, Regex.Escape(keyword), RegexOptions.IgnoreCase)); 

或获取匹配关键字:

var matched = list.Where(keyword => 
       Regex.IsMatch(str, Regex.Escape(keyword), RegexOptions.IgnoreCase)); 
// "Orange", "Tomato" 

BTW,这将同时匹配tomatoesfootomato。如果您需要匹配单词的开头,然后搜索模式应该改变一点点:@"(^|\s)" + keyword

+0

不要忘记在关键字上调用Regex.Escape。如果你有一个特殊的正则表达式字符的关键字,你不会得到一个运行时异常...所以Regex.IsMatch(str,Regex.Escape(关键字)....)。 – jessehouwing

1

对于list.Contains(str),您正在检查list是否包含整个字符串。你需要做的检查有str词语的list什么是这样的:

foreach(var s in list) 
{ 
    if(str.ToLower().Contains(s.ToLower())) 
    { 
      //do your code here 
    } 
} 

将通过列表迭代,并检查您的str,看看它是否在那里。它也将解决你的问题2.由于tomatotomatoes的一部分,它将通过该检查。 ToLower()部分使所有内容都为小写,并且在您想要忽略大小写时常用。

+0

不要使用ToLower将字符串比较,请改用ToUpperInvariant或ToUpper(CultureInfo.CurrentCulture)。请参阅:http://stackoverflow.com/questions/2801508/what-is-wrong-with-tolowerinvariant或使用IndexOf(StringComparisonType)> - 1 http://msdn.microsoft.com/en-us/library/ms224425( v = vs.95).aspx – jessehouwing

3

如果区分大小写是不是你可以做这样的一个问题:

List<string> test = new List<string>(); 
test.Add("Lemon"); 
test.Add("Orange"); 
test.Add("Pepper"); 
test.Add("Tomato"); 

string str = "Today, I ate a tomato and an orange."; 

foreach (string s in test) 
{ 
     // Or use StringComparison.OrdinalIgnoreCase when cultures are of no issue. 
     if (str.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) > -1) 
     { 
      Console.WriteLine("Sentence contains word: " + s); 
     } 
} 

Console.Read(); 
+0

第一:IndexOf> -1,0可以是字符串中的第一个位置。第二:改用str.IndexOf(s,StringComparison.IgnoreCase)。 – jessehouwing

+0

感谢您的编辑,不知道'CurrentCultureIgnoreCase' :) – DGibbs

1
Private Function stringContainsOneOfMany(ByVal haystack As String, ByVal needles As String()) As Boolean 
    For Each needle In needles 
     If haystack.ToLower.Contains(needle.ToLower) Then 
      Return True 
     End If 
    Next 
    Return False 
End Function 

使用方法:

Dim keywords As New List(Of String) From { 
     "Orange", "Lemon", "Pepper", "Tomato"} 
    Dim str As String = "Today, I ate a tomato and an orange" 
    If stringContainsOneOfMany(str, keywords.ToArray) Then 
     'do something 
    End If 
+0

请勿使用ToLower进行字符串比较,请改用ToUpperInvariant或ToUpper(CultureInfo.CurrentCulture)。请参阅:http://stackoverflow.com/questions/2801508/what-is-wrong-with-tolowerinvariant或使用IndexOf(StringComparisonType)> - 1 http://msdn.microsoft.com/en-us/library/ms224425( v = vs.95).aspx – jessehouwing

1
Dim str As String = "Today, I ate a tomato and an orange" 
    Dim sWords As String = "Orange Lemon Pepper Tomato" 
    Dim sWordArray() As String = sWords.Split(" ") 

    For Each sWord In sWordArray 

     If str.ToLower.Contains(sWord.ToLower) Then 
      Console.WriteLine(sWord) 
     End If 

    Next sWord 
+0

请勿使用ToLower进行字符串比较,而应使用ToUpperInvariant或ToUpper(CultureInfo.CurrentCulture)。请参阅:http://stackoverflow.com/questions/2801508/what-is-wrong-with-tolowerinvariant或使用IndexOf(StringComparisonType)> - 1 http://msdn.microsoft.com/en-us/library/ms224425( v = VS.95)的.aspx – jessehouwing

2
Regex reg = new Regex("(Orange|lemon|pepper|Tomato)", RegexOptions.IgnoreCase | RegexOptions.Singleline); 
MatchCollection mc = reg.Matches("Today, I ate tomatoes and an orange."); 
foreach (Match mt in mc) 
{ 
    Debug.WriteLine(mt.Groups[0].Value); 
}