Click here to Skip to main content
Rate this: bad
good
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 21:43pm
Comments
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
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 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
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
0 OriginalGriff 277
1 Maciej Los 220
2 DamithSL 190
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
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