Find occurence in string with line number?

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts Page 1 of 1
Contributors
User avatar
Toxikr3
Dedicated Member
Dedicated Member
Posts: 69
Joined: Tue Dec 08, 2009 12:49 am

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
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

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;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Hello Codenstuff,

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

Thank you.
Image
Lewis
Coding God
Coding God
Posts: 1564
Joined: Sun Dec 20, 2009 2:12 pm

Yes, hope you dont mind but ive used this :D
Image
User avatar
Toxikr3
Dedicated Member
Dedicated Member
Posts: 69
Joined: Tue Dec 08, 2009 12:49 am

Thank you so much!
I will give this ago!

-Toxikr3
5 posts Page 1 of 1
Return to “Tutorial Requests”