简体   繁体   English

如何比较两个字符串的准确性

[英]How to compare two strings for accuracy

I'm making this useless program just to get back into programming properly and I'm struggling with comparing two strings for accuracy.我正在制作这个无用的程序只是为了正确地重新编程,我正在努力比较两个字符串的准确性。


I basically have 2 strings: (example)我基本上有 2 个字符串:(示例)

(Constant that im comparing to) str1 = "abcdefghijkl" (我比较的常数) str1 = "abcdefghijkl"

(Input) str2 = "abcdefghjkli" (输入) str2 = "abcdefghjkli"

The str2 is correct up to (and including) "h". str2 是正确的(包括)“h”。 I want to know what % of the string is correct.我想知道字符串的 % 是正确的。

This is the code I have so far:这是我到目前为止的代码:

Private Function compareString(str1 As String, str2 As String)
'Compares str2 to str1 and returns a % match
Dim strNumber As Integer
Dim percentMatch As Integer
'Dim array1(16), array2(16) As Char
'array1 = str1.ToCharArray
'array2 = str2.ToCharArray

 For x = 0 To str1.Length
    'If array1(x) = array2(x) Then
    If str1(x) = str2(x) Then
        strNumber += 1
    Else
        Exit For
    End If
 Next
 percentMatch = ((strNumber / (str1.Length - 1)) * 100)
 percentMatch = CInt(CStr(percentMatch.Substring(0,4)))
 Return percentMatch

End Function The two commented sections are the other approach I tried before coming here.结束函数 两个注释部分是我来到这里之前尝试的另一种方法。 The code should run as follows代码应该如下运行

compareString("abcdefghijkl", "abcdefghjkli")比较字符串(“abcdefghijkl”,“abcdefghjkli”)

strNum will get to 8. strNum 将达到 8。

percentMatch = ((8 / 12)*100)百分比匹配 = ((8 / 12)*100)

*percentMatch = 75 *百分比匹配 = 75

Return 75返回 75

But, its not returning this, On the lines但是,它没有返回这个,在线

If str1(x) = str2(x) Then

it returns an error, "Index was outside the bounds of the array."它返回一个错误,“索引超出了数组的范围。” I understand the error, just not where I am going wrong.我理解错误,只是不是我出错的地方。

If theres anymore information I can give, I'll do so as soon as I see the notification :)如果我可以提供更多信息,我会在看到通知后立即提供:)

Thanks in Advance,提前致谢,

Rinslep林斯莱普

If you consider the string如果你考虑字符串

str = "ABCDE";

str.Length is 5. But if you index it with a 0 based index, str.Length 是 5。但是如果你用一个基于 0 的索引来索引它,

str[0] = 'A'
...
str[4] = 'E'
'str[5] throws exception (5 = str.Length)

Now in your现在在你

For x = 0 To str1.Length

If you compare with my example, when x is equal to the length of the string, you are checking str[5], which is out of the bound hence throwing the exception.如果您与我的示例进行比较,当 x 等于字符串的长度时,您正在检查 str[5],它超出了范围,因此会引发异常。

Change that line to将该行更改为

Dim shorterLength = IIf(str1.Length < str2.Length, str1.Length, str2.Length); 'So that you cannot go beyond the boundary
For x = 0 To (shorterLength - 1)

Cheers!!!干杯!!!

you need to check the length of given strings also you should not exceed the bound and also don't exit from loop until checking whole string:您需要检查给定字符串的长度,也不应超过界限,并且在检查整个字符串之前也不要退出循环:

Dim x As Integer = 0  
While x < str1.Length AndAlso x < str2.Length
    If str1(x) = str2(x) Then
        strNumber += 1
    End If
    i = i + 1
End While

I know this has been open for a while but I've been looking into this a little.我知道这已经开放了一段时间,但我一直在研究这个。 You would have to respect the length of the strings too.您也必须尊重字符串的长度。 Say you have two strings.假设你有两个字符串。 ABCD and AEF . ABCDAEF AEF is 75% the length of ABCD. AEF 是 ABCD 长度的 75%。 Each letter in ABCD is worth 25%. ABCD 中的每个字母的价值为 25%。 And there's one letter that's correct in AEF and that's A. And as A = 25%: 75% * 25% = 0,75 * 0,25 = 0,1875 = 18,75% . AEF 中有一个字母是正确的,那就是 A。 A = 25%: 75% * 25% = 0,75 * 0,25 = 0,1875 = 18,75% String AEF is 18,75% equal to ABCD.字符串 AEF 是 18.75% 等于 ABCD。

Hope you understood.希望你明白。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM