Home > other >  how to get index of first letter of specific (n-th) word in sentence
how to get index of first letter of specific (n-th) word in sentence

Time:03-31

I am trying to get index of first letter for n-th word in sentence. that word maybe repeated in sentence several times.

for example: I have sentence bla blah bla bla bla

how to get index of first letter in sentence of third "bla" ?

int n=3
string[] words = richTextBox1.Text.Split(new Char[] { ' ' });//richTextBox1.Text="bla blah bla bla bla"
int index= richTextBox1.Text.IndexOf(words[n]);

but it returns index of first "bla" , n=0

in my example it should return index of last bla, 17

CodePudding user response:

Since I don't know where you are getting your word number value, I had to make some assumptions. I built a form with a RichTextBox, a TextBox for reporting the index, and I added a couple of constants to the form. I made notes in the code as to why, and some alternatives.

What I am doing is iterating through the array and keeping two index values. The first index value is the accumulation of all words I have passed-over (including spaces). The second index value is the number of times I have encountered the target word. Once I reach the target value, I update the textbox on the form and break.

public partial class Form1 : Form
{
    const int WORDNUM = 3;
    const char DELIMITER = ' ';
    

    private void button1_Click(object sender, EventArgs e)
    {
        int cnt = 0; //How many times have I encountered the target word?
        //int n = 3; I recommend you retrieve this value from a constant or form. This examples ignores this value and uses a constant.
        int index = 0; //The accumulated index count of all words passed-over (including spaces).

        //Here, I would also put your delimiter in a constant, or allow it to be configured by the user in the form. 
        string[] words = richTextBox1.Text.Split(DELIMITER); //richTextBox1.Text="bla blah bla bla bla"

        for(int i = 0; i < words.Length; i  )
        {
            if (words[i] == WORD && cnt == NTHWORD)
            {
                textBox1.Text = index.ToString();
                break;
            }
            else
            {
                index  = words[i].Length   1;
                if(words[i] == WORD)
                    cnt  ;
            }
        }
    }
}

CodePudding user response:

Your question is a little misleading. You state you want the index of the first letter in the 3rd "bla", implying you want the index of the first letter of the fourth word (@ n = 3). You also state you should get 17 which contradicts the previous statement. If n = 3, the answer should be 13. Try this:

string sentence = "bla blah bla bla bla";
string sentence2 = "The quick brown fox jumped over the lazy dog";
int n = 3; //The fourth word: bla, will return 13
int n2 = 6; //The seventh word: the, will return 32

Console.WriteLine($"{sentence}, n = {n}:\t {GetIndexOfFirstLetterInNthWord(n, sentence)}");
Console.WriteLine($"{sentence}, n = {n}:\t {GetIndexOfFirstLetterInNthWord(n2, sentence2)}");


//@ n = 3, we choose the fourth word, index of first letter == 14
int GetIndexOfFirstLetterInNthWord(int index, string sentence)
{
    int retVal = -1;
    string[] splitSentence = sentence.Split(' ');

    if (index < splitSentence.Length)
    {
        retVal = 0;
        for (int i = 0; i < index; i  )
        {
            retVal  = splitSentence[i].Length   1;
        }
    }
    return retVal;

CodePudding user response:

 string sentence = "bla blah bla bla bla"; // <- your TextBox
        string[] words = sentence.Split(new Char[] { ' ' });
        int n = 3;

        var wordToSearch = words[n]   " ";
        Dictionary<int, string> indexesByWord = new Dictionary<int, string>();

        MatchCollection matches = Regex.Matches(sentence, wordToSearch);

        for (int i = 0; i < words.Count(); i  )
        {
            if (words[i].Equals(words[n]))
            {
                indexesByWord.Add(i, wordToSearch);
            }
        }

        int countDuplicateWordsBeforeN = 0;

        foreach(var i in indexesByWord)
        {
            if(i.Key < n)
            {
                countDuplicateWordsBeforeN  ;
            }
        }

        var index = matches[countDuplicateWordsBeforeN].Index;
  •  Tags:  
  • c#
  • Related