Find words in strings

  • Thread starter Thread starter Paula
  • Start date Start date
P

Paula

Hi !!

I have to find some words in a string.
I can use string.IndexOf, LastIndexOf, etc, but they are case
sensitive.
And there is another problem : If I found the word, I have to get
three words before and after the found word .

Example:

string s = "This is an example about how to find words in a string";
string wordToFind = "Example"

I have to get : This is an example of how to

Can someone help me???

Thanks
 
Paula,

you might want to check System.Text.RegularExpressions namespace and Regex
class. It can split strings using general definition of whitespace and word
boundaries, providing you with collection of indexes for found word - see
Matches, Groups and Captures collections there.

HTH
Alex
 
I can use string.IndexOf, LastIndexOf, etc, but they are case
sensitive.

Just lowercase the string to search and get the positions from there on. Or
use regular expressions as suggested by another poster.
 
Back
Top