none
stringBuilder RRS feed

  • Question

  • Hi All,

    My Question is ;

    How to use Class StringBuilderFinder and Click button event.

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
            Dim StringToCheck As New StringBuilder() ' ****
            For I As Integer = 0 To ListBox2.Items.Count - 1
                StringToCheck.AppendLine(ListBox2.Items(I).ToString) ' ****
            Next I
            StringToCheck = StringToCheck.Replace(vbCrLf, ",").Remove(StringToCheck.Length - 1, 1)
    
            Dim StringsToCheck As String() = StringToCheck.ToString.Split(","c) ' ****
    
    
          For Each S As String In StringsToCheck
                Dim StringsToFind As String() = {S(0) & S(1), _
                                                            S(0) & S(2), _
                                                            S(0) & S(3), _
                                                            S(1) & S(2), _
                                                            S(1) & S(3), _
                                                            S(2) & S(3)}
    
                For Each Find As String In StringsToFind
                    Dim Count As Integer = 0
                    For Each Check As String In StringsToCheck
                        If Check.Contains(Find(0)) AndAlso Check.Contains(Find(1)) Then Count += 1
                    Next
                    TextBox1.AppendText("Occurances (" & S & ", " & Find & ")= " & Count.ToString & vbCrLf)
    
                Next
            Next
    
    
        End Sub
    
    
    End Class
    Public Class StringBuilderFinder
        Private sb As StringBuilder
        Private text As String
    
        Public Sub New(sb As StringBuilder, textToFind As String)
            Me.sb = sb
            text = textToFind
        End Sub
    
        Public Function SearchAndAppend(StringTosearch As String) As Boolean
            sb.Append(StringTosearch)
            Return StringTosearch.Contains(text)
        End Function
    End Class

    Thank.


    • Edited by mipakteh Saturday, March 4, 2017 3:28 AM add
    Saturday, March 4, 2017 3:25 AM

Answers

  • How to use Class StringBuilderFinder and Click button event.

    Change the private variables so that you are not using the same name as the dummy variable in the constructor signature - the existing naming will cause problems.  A common standard is to name the local variables with an initial underscrore, eg _sb and _textToFind.

    To use your class you would instantiate it using a variable local to the calling routine, execute the method and then examine the result returned from the method.   You should describe what you expect the method to do because it isn't clear from the code. Do the asterisk comments indicate something?

    Dim thisSBFinder as New StringBuilderFinder(thisSB, thisText)
    If thisSBFinder.SearchAndAppend(thisSearchString) Then ...

    • Marked as answer by mipakteh Tuesday, March 14, 2017 1:52 PM
    Saturday, March 4, 2017 4:00 AM

All replies

  • Hello,

    Please be more specific in what you are asking.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, March 4, 2017 3:49 AM
    Moderator
  • How to use Class StringBuilderFinder and Click button event.

    Change the private variables so that you are not using the same name as the dummy variable in the constructor signature - the existing naming will cause problems.  A common standard is to name the local variables with an initial underscrore, eg _sb and _textToFind.

    To use your class you would instantiate it using a variable local to the calling routine, execute the method and then examine the result returned from the method.   You should describe what you expect the method to do because it isn't clear from the code. Do the asterisk comments indicate something?

    Dim thisSBFinder as New StringBuilderFinder(thisSB, thisText)
    If thisSBFinder.SearchAndAppend(thisSearchString) Then ...

    • Marked as answer by mipakteh Tuesday, March 14, 2017 1:52 PM
    Saturday, March 4, 2017 4:00 AM
  • Do the asterisk comments indicate something?

    They did when I first posted those lines of code in my example in an earlier thread.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/b21ae722-8f43-4f74-ba1f-4bed0c61bed6/break-that-code?forum=vbgeneral

    They were to alert the OP that these were lines which I had changed/added
    in the OP's original code. The OP does a lot of copy & paste from code
    suggested by others.

    - Wayne

    Saturday, March 4, 2017 5:03 AM
  • I'm remove stringbuilder at the top.Just want to use to append that result into textbox1.Trying follow your suggestion.

    But some word appear at the back of the "Find".

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    
            Dim StringToCheck As String = ""
            For I As Integer = 0 To ListBox1.Items.Count - 1
                StringToCheck &= ListBox1.Items(I).ToString & vbCrLf
            Next I
            StringToCheck = StringToCheck.Replace(vbCrLf, ",").TrimEnd(CChar(","))
    
            Dim StringsToCheck As String() = StringToCheck.Split(","c)
    
    
          For Each S As String In StringsToCheck
                Dim StringsToFind As String() = {S(0) & S(1), _
                                                            S(0) & S(2), _
                                                            S(0) & S(3), _
                                                            S(1) & S(2), _
                                                            S(1) & S(3), _
                                                            S(2) & S(3)}
    
                For Each Find As String In StringsToFind
                    Dim Count As Integer = 0
                    For Each Check As String In StringsToCheck
                        If Check.Contains(Find(0)) AndAlso Check.Contains(Find(1)) Then Count += 1
                    Next
    
                    Dim sb As StringBuilder = New StringBuilder()
                    Dim f As New StringBuilderFinder(sb, Find)
    
                    If f.SearchAndAppend(Find) Then
    
                        sb.Append(f)
    
                        TextBox1.AppendText(sb.ToString & vbCrLf)
                    End If
                Next
            Next
    
    
        End Sub
    
    
    End Class
    Public Class StringBuilderFinder
        Private sb As StringBuilder
        Private text As String
    
        Public Sub New(sb As StringBuilder, textToFind As String)
            Me.sb = sb
            text = textToFind
        End Sub
    
        Public Function SearchAndAppend(Find As String) As Boolean
            sb.Append(Find)
            Return Find.Contains(text)
        End Function
    End Class
    

    Saturday, March 4, 2017 5:11 AM
  • thank Karen..

    Use stringbuilderfinder for showing the Result " S , Find , Count.

    Saturday, March 4, 2017 5:15 AM
  • Actually, I realy use your code:

      Dim StringToCheck As New StringBuilder() ' ****
            For I As Integer = 0 To ListBox2.Items.Count - 1
                StringToCheck.AppendLine(ListBox2.Items(I).ToString) ' ****
            Next I
            StringToCheck = StringToCheck.Replace(vbCrLf, ",").Remove(StringToCheck.Length - 1, 1)
    
            Dim StringsToCheck As String() = StringToCheck.ToString.Split(","c) ' ****
    

    It very fast than other.But now try use string for show the result after find thst string.

    Your code Dict code method...try to learn.

    Thank WayneKing

    Saturday, March 4, 2017 5:23 AM