VB。网络搜索关于句子并返回第一个单词索引

时间:2014-01-03 15:06:41

标签: vb.net linq arabic

我在这里被问到一个问题,但我忘了解释我的问题..所以我的问题是我有一个阿拉伯语的巨大字符串,我想搜索其中的句子,如果我发现它返回第一个字index(单词索引不是字符)..

例如:

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."

所以..当我搜索(程序操纵)我想要返回:1和36 ..最佳方式的任何建议?如果用linq我会赞赏.. thanx

1 个答案:

答案 0 :(得分:1)

你可以使用类似的东西。

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;
        }

    }


}