1

私はlist4つのアイテムを含む文字列を持っています:

Orange Lemon Pepper Tomato

また、私はString str文を持っているを持っています:

Today, I ate a tomato and an orange.

str1)からいくつかのキーワードがあることを確認するにはどうすればよいlistですか?大文字または小文字を考慮せずに、基本的に一致するものをキャプチャしますか?

これを試しましたが、同じ単語を検索するため、機能しません。list.Contains(str)

またDim result As String() = list.FindAll(str, Function(s) s.ToLower().Contains(str))、しかしまた動作しませんでした。

2)単語tomatoが含まtomatoesれている場合、どうすればその部分をstr検出してtomato破棄できesますか?

何か提案やアイデアはありますか?

4

6 に答える 6

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"

ところで、これはとの両方に一致tomatoesfootomatoます。単語の先頭を一致させる必要がある場合は、検索パターンを少し変更する必要があります。@"(^|\s)" + keyword

于 2012-12-11T16:07:03.157 に答える
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();
于 2012-12-11T16:08:46.283 に答える
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);
}
于 2012-12-11T16:11:59.500 に答える
1

を使用すると、文字列全体list.Contains(str)が含まれているかどうかを確認できます。に単語が含まれているlistことを確認するために必要なことは、次のようなものです。strlist

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

それはあなたのリストを繰り返し、strそれがそこにあるかどうかを確認するためにあなたをチェックします。それはあなたの質問2も解決します。tomatoはの一部なのでtomatoes、そのチェックに合格します。このToLower()部分はすべて小文字にし、大文字と小文字を無視したい場合によく使用されます。

于 2012-12-11T16:06:41.457 に答える
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
于 2012-12-11T16:11:19.667 に答える
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
于 2012-12-11T16:11:52.303 に答える