0

I want to get the text between ';' character. However if there are 3 matches like that, i want to get the text between ';' character where current cursor position. I am using multine textbox.

Example;

select * from database;

----> **Lets say my cursor is here** 
select 
orders from
customer;


select * from employees;

So, i only want to get 'select orders from customer' text.

Could you please share your thoughts on it?

2 Answers2

0

To achieve this, you first have to find all indicies of ;. To do this, iterate through all indicies (source):

private List<int> AllIndicesOf(string strToSearch, string fullText)
{
    List<int> foundIndices = new List<int>();
    for (int i = fullText.IndexOf(strToSearch); i > -1; i = fullText.IndexOf(strToSearch, i + 1))
    {
        foundIndices.Add(i + 1);
    }
    return foundIndices;
}

Then you have to compare your position to those indices, since you only want the index (of ;) that follows immediately after your cursor:

List<int> indicies = AllIndicesOf(";", txtBxText.Text);
try
{
    if (indicies.Count > 0)
    {           
        int cursorPos = txtBxText.SelectionStart;
        var indicesBefore = indicies.Where(x => x < cursorPos);
        int beginIndex = indicesBefore.Count() > 0 ? indicesBefore.Last() : 0;
        int endIndex = indicies.Where(x => x > beginIndex).First();
        txtBxSelected.Text = txtBxText.Text.Substring(beginIndex, endIndex - beginIndex);
    }
}
catch { }

The try-catch statement is used to prevent an Exception if your cursors position is after all other indices.

A sample project can be downloaded here.

jAC
  • 5,195
  • 6
  • 40
  • 55
  • wow. this is sooo nice. However, if cursor is middle of the word, it takes from after the cursor to ';' character. What i want is, all text between ';' charachter. –  Jun 03 '17 at 08:56
  • All right, I edited my answer and uploaded a new sample project. – jAC Jun 03 '17 at 09:03
  • Have anyone said that you are best and beast ? –  Jun 03 '17 at 09:04
  • Haha, I don't think I'm the best (not even close), simply trying to help ;-) – jAC Jun 03 '17 at 09:06
0

This solution perfectly works although you need to check it again and consider some possible exceptions. I did not consider them myself because I thought it was better to be handled by you. I also used richTextBox which is better than the multi line text box. Enjoy the code bro

 private void button1_Click(object sender, EventArgs e)
        {
            var ultimateResult = string.Empty;
            var cusrPosition = richTextBox1.SelectionStart;
            var currentStr = string.Empty;
            var strDic = new Dictionary<string,int>();                
            var textArr = richTextBox1.Text.ToCharArray();
            for (var i = 0; i < textArr.Count(); i++)
            {
                if (textArr[i] != ';')
                    currentStr = currentStr + textArr[i];
                else
                {
                    strDic.Add(currentStr,i);
                    currentStr = string.Empty;

                }
            }
            ultimateResult = strDic.First(item => item.Value >= cusrPosition).Key;
            textBox1.Text = ultimateResult;

        }
Mahmood Shahrokni
  • 226
  • 1
  • 3
  • 12