Question

i was asked a question here but i forget to explain my problem well.. so my problem is that I have a huge string in arabic which i want to search about a sentences in it and if i found it return the first word index (word index not character) ..

for example:

Dim myString as String = "Fundamentally programs manipulate numbers and text. These are the building blocks of all programs. Programming languages let you use them in different ways, eg adding numbers, etc, or storing data on disk for later retrieval.. Fundamentally programs manipulate numbers and text."

so.. when i search about (programs manipulate) i want to return: 1 and 36 .. any suggestions by the best way ? if with linq i will appreciated .. thanx

Was it helpful?

Solution

You can use something similar to this.

class Program
{
    public static void Main(string[] args)
    {

        var str = "Fundamentally programs manipulate numbers and text. These are the building blocks of all programs. Programming languages let you use them in different ways, eg adding numbers, etc, or storing data on disk for later retrieval.. Fundamentally programs manipulate numbers and text.";
        foreach (var index in GetIndexes(str, "programs manipulate",' '))
        {
            Console.WriteLine(index);
        }
        Console.ReadKey();
    }


    public static IEnumerable<int> GetIndexes(string str, string search,params char[] delimiters)
    {
        var index = 0;

        var words = str.Split(delimiters).ToList();
        var searchwords = search.Split(delimiters);

        while (words.Any())
        {
            if (words.Take(searchwords.Length).SequenceEqual(searchwords))
                yield return index;

            words.RemoveAt(0);
            index += 1;
        }

    }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top