Page 1 of 1

Find occurence in string with line number?

Posted: Fri Jan 29, 2010 4:00 am
by Toxikr3
Hi guys,
Kind of stuck at this problem for a while...

I am trying to make a basic error checking routine which checks for open brackets or extra brackets.
The idea is to find { bracket with line numbers then find } with line numbers
Then start subtracting each occurrence until you are left with nothing or brackets. If you are left with brackets that mean these are extras or open.

How can I do this... :(

I have a code to find the occurence but I can't seem to get the line numbers. I am using an RTB.
Code: Select all
 Dim strText As String
        Dim strSearchText As String
        Dim strSearchText2 As String
        Dim index As Integer
        Dim count As Integer
        Dim count2 As Integer
        strText = RTB.Text
        strSearchText = "{"
        strSearchText2 = "}"
        count = 0
        count2 = 0
        For index = 0 To strText.Length - 1
            If strText.Substring(index, 1) = strSearchText Then
                count = count + 1
            End If
        Next

        For index = 0 To strText.Length - 1
            If strText.Substring(index, 1) = strSearchText2 Then
                count2 = count2 + 1
            End If
        Next
        Dim h As Integer

        h = count - count2
        If h <= -1 Then
            ListBoxErrors.Items.Add("Missing {")
        End If
Hope some can help

-Toxikr3

Re: Find occurence in string with line number?

Posted: Fri Jan 29, 2010 5:12 am
by CodenStuff
Hello,

You can get the current line number from a richtextbox, for example show it in a messagebox:
Code: Select all
MsgBox(RichTextBox1.GetLineFromCharIndex(RichTextBox1.SelectionStart).ToString())
Oh this will actually give you the exact line number of each occurrence, I put them into the ListBox while I was testing:
Code: Select all
ListBox1.Items.Add(RichTextBox1.GetLineFromCharIndex(index).ToString())
I put it in your code like this:
Code: Select all
For index = 0 To strText.Length - 1
            If strText.Substring(index, 1) = strSearchText Then
                count = count + 1
                ListBox1.Items.Add(RichTextBox1.GetLineFromCharIndex(index).ToString())
            End If
        Next
Hope that helps cooll;

Re: Find occurence in string with line number?

Posted: Fri Jan 29, 2010 8:16 am
by Usman55
Hello Codenstuff,

That was a nice code snippet. You should paste it in the Quick Snips section.

Thank you.

Re: Find occurence in string with line number?

Posted: Fri Jan 29, 2010 9:24 pm
by Lewis
Yes, hope you dont mind but ive used this :D

Re: Find occurence in string with line number?

Posted: Sat Jan 30, 2010 12:06 am
by Toxikr3
Thank you so much!
I will give this ago!

-Toxikr3