VBScript:如何找到两个看起来相同的字符串之间的差异

发布于 2024-09-08 15:49:37 字数 265 浏览 3 评论 0原文

在 VB 脚本中比较两个字符串“test_name”。它们必须相同,并且在调试查看器中看起来相同,但 StrCompare(string1, string2) 返回 1。

历史记录。 这是QTP中的测试。第一个字符串是从 Excel 中读取的。第二个来自 Windows 应用程序。 QTP 从 Excel 读取一个值,将其输入到 Windows 窗体中,然后从另一个地方读取相同的值。如果这两个值相同,则测试通过。

如何找到这两个字符串的差异以便我可以纠正测试?

There are two strings "test_name" that are compared in a VB script. They have to be identical, and they look identical in debug viewer, but StrCompare(string1, string2) returns 1.

History.
This is a test in QTP. The first string is read from Excel. The second one is from a windows application. QTP reads a value from Excel, enters it to a windows form, and then reads the same value from another place. The test passes if these two values are identical.

How to find a difference in these two strings so that I can correct the test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请远离我 2024-09-15 15:49:37

我建议使用 For 循环、MidAsc 来一一比较实际字符。像(未尝试的代码)这样​​的东西:

' Presume input strings named s1 and s2
' Assume Len(s1) = Len(s2)
Dim i

For i = 1 to Len(s1)
   If Asc(Mid(s1, i, 1)) <> Asc(Mid(s2, i, 1)) Then
        Msgbox "Strings differ at character " & i
    End If
Next 'i

如果通过这个测试它们相等,并且通过 StrComp 不相等,那么......我真的不知道。也许对 LenBAscB 尝试同样的操作,看看是否是 Unicode 或编码问题。

I would suggest using a For loop, Mid, and Asc, to compare the actual characters one by one. Something like (untried code):

' Presume input strings named s1 and s2
' Assume Len(s1) = Len(s2)
Dim i

For i = 1 to Len(s1)
   If Asc(Mid(s1, i, 1)) <> Asc(Mid(s2, i, 1)) Then
        Msgbox "Strings differ at character " & i
    End If
Next 'i

If they are equal by this test, and unequal by StrComp then... I don't really know. Perhaps try the same thing with LenB and AscB to see if it's a Unicode or encoding issue somehow.

£噩梦荏苒 2024-09-15 15:49:37

最有可能的是你在末尾有尾随空格(或者其他像空格一样打印的东西)。尝试像这样打印它们:

Debug.Print "*" & string1 & "*"
Debug.Print "*" & string2 & "*"

看看你会得到什么。

Most likely you have trailing spaces at the end (or something else that prints like a space). Try to print them like this:

Debug.Print "*" & string1 & "*"
Debug.Print "*" & string2 & "*"

and see what you get.

吹梦到西洲 2024-09-15 15:49:37

您是否尝试在 StrComnpare 中使用参数 vbTextCompare
这将对两个字符串进行不区分大小写的比较。

我也会推荐上面2位所说的。

所以应该是:

StrCompare(String1, String2, vbTextCompare)

亲切的问候,

Did you try using the parameter vbTextCompare in your StrComnpare ?
This would do case-insensitive comparison of both strings.

I also would have recommended what the 2 above said.

So it would be:

StrCompare(String1, String2, vbTextCompare)

Kind Regards,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文