Something like this might work for you (note that you might want to make the search case-insensitive as well).
using System;
using System.Collections.Generic;
using System.Linq;
namespace Words
{
class Program
{
static IEnumerable<int> IndexOf(IEnumerable<string> list, string searchItem)
{
var index = 0;
foreach (var item in list.Select(w => w.Trim('.', ',', ';', '(', ')', '[', ']')))
{
if (item.Equals(searchItem))
yield return index;
++index;
}
}
static void Main(string[] args)
{
var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis erat diam, vel placerat";
var words = text.Split(' ');
var searchWord = "dolor";
foreach (var index in IndexOf(words, searchWord))
{
var before = words.Take(index).Skip(index - 5).Take(5);
var after = words.Skip(index + 1).Take(5);
Console.WriteLine(String.Join(" ", before) + " " + String.Join(" ", after));
}
}
}
}
Hope this helps,
Fredrik