Find a Word in a String - February 2015

If you download this tip please consider making a modest donation.

Why Write this Code - One application I'm working on needed to be able to search a library of text using search words. One of the problems was that I could not just use a compare search because the search was not case-insensitive. So after a search of the web I found that Asp.Net provided a search called IndexOf with operand StringComparison.CurrentCultureIgnoreCase. I wrote a demo app that shows how to use it. Click here to link to the demo app.

Demo App - The demo application loads some text from the US Constitution. Search words are entered in the Search Words text box. The Do the Search button is clicked and the found words are shown in red. The number of occurrences, of each word, are shown in the Search Results text box. You can change the search words, click the button and find the words in the text. Try it.

The text to be searched. Found text is shown in red.

The words to be used in the search.

The results.

Click to search.

Operative Code

The key is using the IndexOf function with StringComparison.CurrentCultureIgnoreCase

Calling the Search function.

Download the VB.Net Code

Download the source code by clicking on the zipped file name. Click here to run the demo application.

Download - Source Zipped File Size
VS/2008 VB.Net, ASP.Net Solution 50K

Previous Tip    Tip Index   Next Tip


Updated 2016/12/02