Hello, i have 2 textbox.
and i need to compare those 2 strings to get the number of same
letters/characters.
Example:
Hack
Cake

Total same letters/characters = 3 [ C.K.A ]

How can i do that?
Thanks! :(

Recommended Answers

All 10 Replies

If you turn your string into a list/array, then you can use Linq to intersect.

Im a begginer in vb.net, can you explain it's basic usage.

Tried this one, gives me an error

    first = FlatTextBox1.Text
        second = FlatTextBox2.Text

        x = first.ToCharArray()
        y = second.ToCharArray()
        Dim intersection As IEnumerable(Of Integer) = x.Intersect(y)
        For Each total As Integer In intersection
            c = c + 1
        Next
        FlatLabel1.Text = c

O.K. put a button on your form and paste the code into it.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim word1 As String = TextBox1.Text
        Dim i As Integer
        For i = 0 To word1.Length - 1
            MsgBox(word1.Chars(i))
        Next
    End Sub

This runs through the word in textbox1 and displays every charcter in a msgbox until it wnt through the whole word. The rest you should be able to do yourself

What i need is, to calculate how many same letters are there in the 2 strings
Eg :
Hello
Hell
Same letters = Hell
count how many letters : 4

You should be comparing chars, not ints.

I need an int result so i could, make a select statement.
select case result

case 1
label1.text = "friends"
case 2
label1.text = "Lovers"
end select

Like that.

I need an int result so i could, make a select statement.

That comes after the intersect, by using Count() on the resulting list.

ignnniter, see if this helps you get your head around it.
I have a button and two textboxes on a form use the default names in the click event put this.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Dim lstChars1() As Char = TextBox1.Text.ToCharArray
        Dim lstchars2() As Char = TextBox2.Text.ToCharArray
        Dim ch As IEnumerable(Of Char) = lstChars1.Intersect(lstchars2)

        Dim sb As New System.Text.StringBuilder
        For Each c In ch
            sb.Append(c & ", ")
        Next
        sb.Remove(sb.Length - 2, 2)

        MsgBox("There are " & ch.Count & " matching characters" & vbCrLf & sb.ToString)
    End Sub

I was a little too hasty with my previous post it should have been:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Dim lstChars1() As Char = TextBox1.Text.ToCharArray
        Dim lstchars2() As Char = TextBox2.Text.ToCharArray
        Dim ch As IEnumerable(Of Char) = lstChars1.Intersect(lstchars2)

        Dim out As String = String.Join(",", ch)

        MsgBox("There are " & ch.Count & " matching characters" & vbCrLf & out)
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.