我需要比较VB中两个字符串中的字符,并获得不同字符数量的数值。我使用这个是为了通过检查差异的数量来看两个单词的拼写是否相同,但是如果它们相似则我必须给予信任 - 因此需要的数值。非常感谢!
一些可能使事情更清晰的伪代码:
Dim string1, string2 As String
Dim change As Integer
[On button press]
string1 = TextBox1.Text
string2 = TextBox2.Text
CompareCharacters(string1, string2)
FormatNumber of differences between strings = change
MsgBox(change)
非常感谢。
答案 0 :(得分:0)
使用LINQ:
Friend Function AmountOfDifferentCharacters(ByVal String1 As String,
ByVal String2 As String) As Integer
Return (From c As Char In String2 Where Not String1.Contains(c)).Count
End Function
答案 1 :(得分:-1)
您可以遍历字符串并比较每个字符:
Dim cnt As Integer = 0
For index = 0 to Math.Min(string1.Length, string2.Length) - 1
If string1.Substring(index, 1) <> string2.Substring(index, 1) Then
cnt = cnt + 1
End If
Next
Dim change As String = cnt.ToString()