Click here to Skip to main content
15,906,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

:)
Posted

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

Here it is again...

C#
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);
}
 
Share this answer
 
Comments
StM0n 27-Dec-12 5:28am    
Jeeeez... I'm totally muddled up... hope this will help
this can also give desired output...
C#
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!
:)
 
Share this answer
 
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...
C#
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
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!
 
Share this answer
 
Comments
StM0n 27-Dec-12 6:14am    
Yeah, as I mentioned earlier, there're a lot of possible solutions :) we should make a collection of these...

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