如何制作一个程序来检测两个字符串中的相同字符

时间:2015-01-22 15:50:53

标签: vb.net visual-studio-2010 loops

所以我做了这个,但是当我输入一个字符串时,它只能检测到一个字符 并且它也不会将输入的字符串转换为小写

    Dim readme, readme2 As String
    Dim j, i As Integer 
    Dim Compare As Integer
    readme = TextBox1.Text
    readme2 = TextBox2.Text

    readme.ToLower.Substring(i, readme.Length)
    readme2.ToLower.Substring(j, readme2.Length)


    For i = 0 To readme.Length

        For j = 0 To readme2.Length

            If readme = readme2 Then
                Compare = +1

            End If
        Next
    Next


    Label4.Text = Compare`enter code here`

2 个答案:

答案 0 :(得分:1)

字符串是不可变的。您不能将方法应用于字符串,并期望该字符串更改以响应该方法的内部操作 您需要将操作的结果重新分配给您用于调用方法的相同字符串

readme = readme.ToLower()
readme2 = readme2.ToLower()

你问题的第二部分更加困惑,你是否试图计算同一位置的平等字符数?

在这种情况下,你的循环应该是

Dim maxLenToCheck = Math.Min(readme.Length, readme2.Length)
For i = 0 To maxLenToCheck - 1
    If readme(i) = readme2(i) Then
        Compare += 1
    End If
Next

在该循环中,您始终将Compare设置为1,正确的语法是递增Compare变量

Compare += 1

按照下面的评论,我假设您的循环应该写为

Dim Compare = 0
For i = 0 To readme.Length - 1
    for j = 0 to readme2.Length -1 
        If readme(i) = readme2(j) AndAlso _
           Not Char.IsWhiteSpace(readme(i)) Then
            Compare += 1
        End If
    Next
Next

答案 1 :(得分:0)

根据Steve在解决方案中的评论,作者希望知道两个字符串中出现的字母数,忽略大小写和空格。

然而,根据我的计算,解决方案应该是21,而不是20.这是一个使用LINQ的解决方案,它也提供了这些字母所在位置的可视反馈:

Solution via LINQ w/ Visual Feedback

Public Class Form1

    Private Class LetterCount

        Public Letter As Char
        Public Count As Integer

        Public Overrides Function ToString() As String
            Return Letter & " : " & Count
        End Function

    End Class

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = "This is a Test"
        TextBox2.Text = "This should be tryed before"
        RichTextBox1.ReadOnly = True
        RichTextBox1.Font = New Font("MS Courier", 14) ' monospaced font
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RichTextBox1.Text = TextBox1.Text & vbCrLf & TextBox2.Text & vbCrLf

        Dim charsA As New List(Of Char)(TextBox1.Text.ToLower.ToCharArray.Where(Function(x) Not Char.IsWhiteSpace(x)))
        Dim charsB As New List(Of Char)(TextBox2.Text.ToLower.ToCharArray.Where(Function(x) Not Char.IsWhiteSpace(x)))

        Dim DistinctCommonLetters = (From A In charsA, B In charsB Where A = B Select A).Distinct
        Dim DistinctCounts =
            From Letter In DistinctCommonLetters, C In charsA.Concat(charsB)
            Where C = Letter
            Group By Letter Into Group
            Select New LetterCount With {.Letter = Letter, .Count = Group.Count}
        Dim TotalMatches = DistinctCounts.Sum(Function(x) x.Count)

        ListBox1.DataSource = DistinctCounts.ToList
        Label1.Text = "TotalMatches: " & TotalMatches
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim LC As LetterCount = ListBox1.SelectedItem

        RichTextBox1.SelectAll()
        RichTextBox1.SelectionColor = Color.Black

        Dim index As Integer = RichTextBox1.Find(LC.Letter, 0, RichTextBoxFinds.None)
        While index <> -1
            RichTextBox1.Select(index, 1)
            RichTextBox1.SelectionColor = Color.Red
            index = RichTextBox1.Find(LC.Letter, index + 1, RichTextBoxFinds.None)
        End While
    End Sub

End Class