Click here to Skip to main content
15,885,058 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
lukeer 14-Feb-13 3:01am    
You are already showing code in your question. Are you looking for the String.IndexOf()[^] method or do you want to implement a string matching algorithm yourself?

The best way to achieve that is to use RegEx[^]. At the end of the article, you'll find examples.
 
Share this answer
 
v2
Comments
CHill60 14-Feb-13 9:17am    
Hi Maciej ... I agree RegEx as the solution but your link wasn't working ... getting Content Not Found ... I think you need .aspx not .asp
Maciej Los 14-Feb-13 9:25am    
Thank you for the information and suggestion ;)
Now should be OK.
CHill60 14-Feb-13 9:30am    
It is indeed. My 5
Maciej Los 14-Feb-13 10:45am    
Thank you ;)
if you want to search a key in a text box you can do like this:

C#
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
}
 
Share this answer
 
v2
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:

C#
Regex.Match(content, @"\b" + keywords + @"\b", RegexOptions.Singleline | RegexOptions.IgnoreCase);
//Assuming keyword is a string you want to search


I did not write the code myself, source is here: http://stackoverflow.com/questions/1209049/regex-match-whole-words[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900