-3

「 cat」= * cat、c*at、ca*t、cat*. 私のコードは.

次に例を示します。

string s = "the cat is here with the cagt";
int count;

string[] words = s.Split(' ');
foreach (var item in words)
{
    if(///check for "cat")
    {
        count++;
        return count; (will return 2)
    }
}
4

4 に答える 4

1

これはあなたが望んでいないことをしますが、それでも SpellCheck ライブラリが進むべき道だと思います

string wordToFind = "cat";
string sentance = "the cat is here with the cagt";
int count = 0;

foreach (var word in sentance.Split(' '))
{
    if (word.Equals(wordToFind, StringComparison.OrdinalIgnoreCase))
    {
        count++;
        continue;
    }
    foreach (var chr in word)
    {
        if (word.Replace(chr.ToString(), "").Equals(wordToFind, StringComparison.OrdinalIgnoreCase))
        {
            count++;
        }
    }
}

// returns 2
于 2013-01-06T08:19:23.157 に答える
0

これは単純で正しいですが、遅いです:

static bool EqualsExceptOneExtraChar(string goodStr, string strWithOneExtraChar)
{
  if (strWithOneExtraChar.Length != goodStr.Length + 1)
    return false;
  for (int i = 0; i < strWithOneExtraChar.Length; ++i)
  {
    if (strWithOneExtraChar.Remove(i, 1) == goodStr)
      return true;
  }
  return false;
}
于 2013-01-06T09:22:04.840 に答える
0

おそらく、検索マッチングには正規表現を使用できます。

正規表現クラス MSDN

C# 正規表現

30 分間の正規表現チュートリアル

于 2013-01-06T08:13:15.360 に答える
0

正規表現が最良の解決策になる可能性があります。しかし、これも試してみてください。

String str = yourstring;
String s1 = 'cat';
 int cat1 = yourstring.IndexOf(s1,0); 
于 2013-01-06T07:54:16.667 に答える