Click here to Skip to main content
11,789,706 members (61,052 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
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 13-Feb-13 20:43pm
lukeer at 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?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The best way to achieve that is to use RegEx[^]. At the end of the article, you'll find examples.
CHill60 at 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 at 14-Feb-13 9:25am
Thank you for the information and suggestion ;)
Now should be OK.
CHill60 at 14-Feb-13 9:30am
It is indeed. My 5
Maciej Los at 14-Feb-13 10:45am
Thank you ;)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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!",
    this.EditFindNext.Enabled = true;
    this.TextBox.SelectionStart = index;
    this.TextBox.SelectionLength = key.Length;
   //keep these indexes
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You should add the whole word in your regular expression like this:

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

I did not write the code myself, source is here:[^]

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

  Print Answers RSS
0 OriginalGriff 999
1 Maciej Los 765
2 KrunalRohit 686
3 CPallini 606
4 Richard MacCutchan 430

Advertise | Privacy | Mobile
Web02 | 2.8.1509028.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100