C#搜索字符串中出现的单词

C#搜索字符串中出现的单词,c#,C#,如何在字符串中搜索单词? 我有一个文本框,用户在其中插入一个字符串,另一个文本框中只插入一些随机文本。 除了使用regex和IndexOf之外,还有其他方法可以做到这一点吗?例如使用循环并检查单词的长度和字符 int i = 0; int count = 0; input2.Trim(); while ((i = input2.IndexOf(input1, i)) != -1) {

如何在字符串中搜索单词? 我有一个文本框,用户在其中插入一个字符串,另一个文本框中只插入一些随机文本。 除了使用
regex
IndexOf
之外,还有其他方法可以做到这一点吗?例如使用循环并检查单词的长度和字符

        int i = 0; 
        int count = 0;

        input2.Trim(); 

        while ((i = input2.IndexOf(input1, i)) != -1) 
        {
            i = i + input1.Length;
            count++; 
        }

        MessageBox.Show(count.ToString() + " Matches Found");
这就是我到目前为止所尝试的

        int i = 0; 
        int count = 0;

        input2.Trim(); 

        while ((i = input2.IndexOf(input1, i)) != -1) 
        {
            i = i + input1.Length;
            count++; 
        }

        MessageBox.Show(count.ToString() + " Matches Found");

使用正则表达式匹配发生次数

        int i = 0; 
        int count = 0;

        input2.Trim(); 

        while ((i = input2.IndexOf(input1, i)) != -1) 
        {
            i = i + input1.Length;
            count++; 
        }

        MessageBox.Show(count.ToString() + " Matches Found");
string test = "THE DOG WENT TO TOWN DOG";
int j = Regex.Matches(test, "DOG").Cast<Match>().Count();
string test=“这只狗去城里了”;
int j=Regex.Matches(test,“DOG”).Cast().Count();

使用正则表达式匹配发生次数

        int i = 0; 
        int count = 0;

        input2.Trim(); 

        while ((i = input2.IndexOf(input1, i)) != -1) 
        {
            i = i + input1.Length;
            count++; 
        }

        MessageBox.Show(count.ToString() + " Matches Found");
string test = "THE DOG WENT TO TOWN DOG";
int j = Regex.Matches(test, "DOG").Cast<Match>().Count();
string test=“这只狗去城里了”;
int j=Regex.Matches(test,“DOG”).Cast().Count();

看起来您想要获取文本中搜索字符串的计数。您可以尝试以下方法

        int i = 0; 
        int count = 0;

        input2.Trim(); 

        while ((i = input2.IndexOf(input1, i)) != -1) 
        {
            i = i + input1.Length;
            count++; 
        }

        MessageBox.Show(count.ToString() + " Matches Found");
string searchString = "TEST";
string completeText = "Some sentence TEST with other words incluing TEST";
int count = completeText.Split(new string[]{"TEST"},StringSplitOptions.None)
                        .Count() - 1;
MessageBox.Show(count.ToString() + " Matches Found");

看起来您想要获取文本中搜索字符串的计数。您可以尝试以下方法

        int i = 0; 
        int count = 0;

        input2.Trim(); 

        while ((i = input2.IndexOf(input1, i)) != -1) 
        {
            i = i + input1.Length;
            count++; 
        }

        MessageBox.Show(count.ToString() + " Matches Found");
string searchString = "TEST";
string completeText = "Some sentence TEST with other words incluing TEST";
int count = completeText.Split(new string[]{"TEST"},StringSplitOptions.None)
                        .Count() - 1;
MessageBox.Show(count.ToString() + " Matches Found");

您正在使用
IndexOf
-这没有使用regex。@Oded我知道^^我只是想知道是否还有其他方法可以做到这一点?@Oded您是对的。它不。。。删除我的评论。。。我想我的大脑有点僵硬或者类似的情况。你为什么要寻找其他的方法呢?您现在的代码有什么问题?我的代码没有问题,我只是想知道,例如,您是否可以使用类似for循环的东西。对于(int i=0;iIndexOf-这不是使用正则表达式。@Oded我知道^^我只是想知道是否还有其他方法可以做到这一点?@Oded您是对的。它不。。。删除我的评论。。。我想我的大脑有点僵硬或者类似的情况。你为什么要寻找其他的方法呢?您现在的代码有什么问题?我的代码没有问题,我只是想知道,例如,您是否可以使用类似for循环的东西。对于(inti=0;iIndexOf
Regex
的替代方案。该代码显然旨在统计出现的次数。这没用-编辑1个。OP要求提供
IndexOf
Regex
的替代方案。哈,我没想到这一点。做事总有另一种方法。哈,我没想到这一点。做事总有另一种方法。