如何在C#中获得第n个单词索引 [英] How to get nth word index in C#

查看:72
本文介绍了如何在C#中获得第n个单词索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





请问如何在C#中获得第n个单词索引,我试过但是我无法得到它。





例如:我要去学校我要去学校



在上面的字符串going和School从他们重复我希望得到特定的单词索引,如FirstSchool单词索引或SecondSchool单词索引。





请建议我。



谢谢



:)

Hi all,


Please how can I get nth word index in C# , I tried but I am unable to get It.


For Example : I am going to School I am going to School

In above string "going" and "School" is repeated from them I want to get specific word index like First "School" word index or Second "School" word index.


Please Suggest me.

Thanks

:)

推荐答案

Argh ....该死的...对不起......意外删除了解决方案



又来了......



Argh.... damn... sorry... accidently deleted the solution

Here it is again...

var myString = "I am going to School I am going to School";

//Whatever should split the string...
var words = myString.Split(' ');

//Mind that the indexes are zero-based
var wordsAndIndexes = new Dictionary<String, List<int>>();

for(var index = 0; index < words.Length; index++) {

    var word = (String)words.GetValue(index);

    if (!wordsAndIndexes.ContainsKey(word)) {
        wordsAndIndexes.Add(word, new List<int>());
    }

    wordsAndIndexes[word].Add(index);

}

var indexesForSchool = wordsAndIndexes["I"];

foreach(var index in indexesForSchool) {
    WL(index);
}


这也可以提供所需的输出...

this can also give desired output...
string statement = "I am going to School I am going to School";
// Call Regex.Matches method.
MatchCollection matches = Regex.Matches(statement, "School");

string output = "";

// Loop over matches.
foreach (Match m in matches) 
{
    // Loop over captures.
    foreach (Capture c in m.Captures) 
        {
    	// Display.
    	output += c.Index.tostring() + ",";
    }
}



快乐编码!

:)


Happy Coding!
:)


这个并不像它看起来那么简单 - 如果你可以在MatchCollection上使用Linq它会更容易......

This is not as simple as it looks to start with - if you could use Linq on a MatchCollection it would be a whole lot easier...
string inp = "I am going to School I am going to School";
Regex words = new Regex(@"\w+");
MatchCollection matches = words.Matches(inp);
Dictionary<string, List<int>> indexesByWord = new Dictionary<string, List<int>>();
foreach (Match m in matches)
    {
    string word = m.Value;
    if (!indexesByWord.ContainsKey(word))
        {
        indexesByWord.Add(word, new List<int>());
        }
    indexesByWord[word].Add(m.Index);
    }
foreach (var word in indexesByWord)
    {
    Console.WriteLine(word.Key);
    foreach (int index in word.Value)
        {
        Console.WriteLine("   {0}", index);
        }
    }

将输出:

Will output:

I
   0
   21
am
   2
   23
going
   5
   26
to
   11
   32
School
   14
   35

这使用正则表达式将字符串分解为单个单词,然后解析匹配,按字词对它们进行分组,每个单词都得到它的集合索引。

正如我所说 - 如果你可以使用Linq它会更容易!

This uses a regex to break the string into individual words, then parses the matches, grouping them by word with each word getting a collection of it''s indexes.
As I say - if you could use Linq it would be a lot easier!


这篇关于如何在C#中获得第n个单词索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆