在文章中找到这个词 [英] find the word in the article
本文介绍了在文章中找到这个词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好所有
我想在文章中询问一个单词搜索
我有一串文字
假设我有一个很长的文章,我想找个单词。
hallo all
I want to ask about a word search in article
I have a string of text
suppose I have a long article, I want to find a word.
string findText = @"word_wide , most_mind , lemon_tea";
if (findText.Contains("word"))
findText = findText.Substring(0, findText.IndexOf("word")); ;
如何制作代码。
how the code should be made.
推荐答案
最好的实现这一目标的方法是使用 RegEx [^] 。在文章的最后,你会找到例子。
The best way to achieve that is to use RegEx[^]. At the end of the article, you''ll find examples.
如果你想在文本框中搜索一个键,你可以这样做:
if you want to search a key in a text box you can do like this:
private void FindForText(string key, int startIndex)
{
int index = this.TextBox.Text.IndexOf(key, startIndex);
if (index == -1)
{
this.EditFindNext.Enabled = false;
MessageBox.Show("Match not found!",
"Find",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
this.EditFindNext.Enabled = true;
this.TextBox.SelectionStart = index;
this.TextBox.SelectionLength = key.Length;
this.TextBox.ScrollToCaret();
//keep these indexes
}
你应该在正则表达式中添加整个单词,如下所示:
(鞋子|衬衫|裤子)
鞋子,短裤或裤子是你要搜索的词。
代码示例是这样的:
You should add the whole word in your regular expression like this:
(shoes|shirt|pants)
Where shoes, short or pants are the words you want to search.
code example is such:
Regex.Match(content, @"\b" + keywords + @"\b", RegexOptions.Singleline | RegexOptions.IgnoreCase);
//Assuming keyword is a string you want to search
我自己没有编写代码,源代码在这里: http://stackoverflow.com/questions/1209049/regex-match-whole-words [ ^ ]
这篇关于在文章中找到这个词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文