Click here to Skip to main content
13,148,224 members (47,124 online)
Rate this:
Please Sign up or sign in to vote.
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 13-Feb-13 20:43pm
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?
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 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 ;)
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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2017
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