Click here to Skip to main content
12,949,700 members (71,747 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
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?
Rate this: bad
 
good
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.
  Permalink  
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 ;)
Rate this: bad
 
good
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!",
            "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
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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


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

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
OriginalGriff 5,489
CHill60 3,380
Maciej Los 2,913
Jochen Arndt 1,935
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web02 | 2.8.170524.1 | 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