-3

I need to find the repeated words exact index in a text. For example, refer the below text.

string text = "The first text area sample uses a dialog text to display the errors";
text.IndexOf("text");

In this string the word "text" is repeated twice. I need to get the index of both positions. If we using the "IndexOf" as in the above code will return the 10 always, which is the index of the 1st word "text". So, how do we find the exact index of the repeated words in a text using C#.

velmurugan
  • 66
  • 7

2 Answers2

4

Do it in a loop, C#

string text = "The first text area sample uses a dialog text to display the errors";
int i = 0;
while ((i = text.IndexOf("text", i)) != -1)
{
    // Print out the index.
    Console.WriteLine(i);
    i++;
}

JavaScript

var text = "The first text area sample uses a dialog text to display the errors";
var i;
while ((i = text.IndexOf("text", i)) != -1)
{
    // Print out the index.
    alert(i);
    i++;
}
Paul Swetz
  • 2,234
  • 1
  • 11
  • 28
  • `i` does not exist in current context? Where was `i` declared/initialized? – Grizzly Jun 21 '16 at 18:48
  • That update will still throw a compile error. `int` is a type which is not valid in given context.. `int i = 0;` needs to be declared outside of the loop – Grizzly Jun 21 '16 at 18:53
  • I don't mean to annoy, your answer is correct, I just like to see that all areas are correct. good job! – Grizzly Jun 21 '16 at 18:55
  • If i have multiple same words and need to find index means, how to achieve. For example, in the above text "the" also repeated word. So, need to find the index of this word also. Similarly, i may have n number of repeated words in a given string, that time how to find the index? – velmurugan Jun 21 '16 at 18:58
  • Every word needs its own loop. This really does become a duplicate of http://stackoverflow.com/questions/3410464/how-to-find-all-occurrences-of-one-string-in-another-in-javascript – Paul Swetz Jun 21 '16 at 19:01
  • @Paul Swetz thanks. If we have more than 20 repeated words, is that fair to maintain own loop for that words. And if we doesn't know those repeated words how do we find the index. Since, i may have a paragraph with 100 lines and it may have more repeated words. What we will do in this case? It is difficult to check and mark the repeated words and write a own loop for that. – velmurugan Jun 21 '16 at 19:16
  • You should really ask a new question, you started small and are trying to blow it out of scope. Ask a new question showing the work you have. – Paul Swetz Jun 21 '16 at 19:20
0

This is a possible duplicate question for a javascript solution (which can work in any language):

Here is the solution given from the other post:

function getIndicesOf(searchStr, str, caseSensitive) {
  var startIndex = 0, searchStrLen = searchStr.length;
  var index, indices = [];
  if (!caseSensitive) {
     str = str.toLowerCase();
     searchStr = searchStr.toLowerCase();
  }
  while ((index = str.indexOf(searchStr, startIndex)) > -1) {
     indices.push(index);
     startIndex = index + searchStrLen;
  }
  return indices;
}

getIndicesOf("le", "I learned to play the Ukulele in Lebanon.", false);
Community
  • 1
  • 1
Jay
  • 2,656
  • 1
  • 16
  • 24