Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text very longer now in text, I search one word so I wanna get after/before 5 word that word For example
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis venenatis erat diam, vel placerat

i search 'elit' but before/after 5 word according to that word

dolor sit amet, consectetur adipiscing elit. Duis venenatis erat diam, vel
Like this

but also can be not enough 5 word

For example I search 'dolor' above the text. but there are only 2 word before dolor

so

ı want to get
Lorem ipsum dolor sit amet, consectetur adipiscing elit

like this

how can i do it with C#
Posted
Comments
Sushil Mate 12-Jul-13 0:34am    
spilt the sentence by spaces, dot, comma what not & put it into array. search in the array where ever you find that word take next 5 elements from the array.

Something like this might work for you (note that you might want to make the search case-insensitive as well).

C#
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('.', ',', ';', '(', ')', '[', ']'))) // Get rid of trailing full stops etc
            {
                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";

            // Loop here because the search word might appear more than once in the text
            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
 
Share this answer
 
Thank you a lot,
and also I have learnt a answer, we can find use regex how?

C#
List<string> listSentences = new List<string>();
                    string pattern = "(?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,10}" + word +
                                     "(?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,10}";

                    MatchCollection matches = Regex.Matches(text.ToLower(), pattern);
                    for (int i = 0; i < matches.Count; i++)
                    {
                        string s = "<ul><li>" + matches[i].ToString() + "</li></ul>";
                        listSentences.Add(s.Replace(word, "<span style="color: red;">"+word+"</span>"));
                    }</string></string>


thank you !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900