我这样做是正确的Strcomp值...... [英] Am I doing this right Strcomp values...

查看:127
本文介绍了我这样做是正确的Strcomp值......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是否需要比较两个值(作为字符串)并使用VB.Net中的StrComp()函数,如下所示:

 ResultReadBackTest = StrComp(ValResultPastEquals,Value_Sx_Calculated_String)



它的作用是检查读回的值从设备中将其与发送的内容进行比较。

设备只处理字符串。



但是我可以看到这个方法的问题因为我已经抨击了一个快速的程序来测试



  Dim  ValueA 作为 字符串 
Dim ValueB 作为 字符串
Dim 结果作为 整数


ValueA = 0011059096
ValueB = 11059096

结果= StrComp(ValueA,ValueB)
MsgBox( Result = + Result.ToString())



我从程序中获得的值我写的是0011059096和11059096

给出1,意思是从MSDN页面Value1排序后Value2 ??

我知道如果我做了一个子串并且摆脱了领先的零它可能会起作用,但这可能是危险的。请注意以下内容:

 intValA = Val(ValueA)
intValB = Val(ValueB)
MsgBox( 值A = + intValA.ToString()+ < span class =code-string>值B = + intValB.ToString())
如果(intValA = intValB)然后 MsgBox( Huzzah

或者把它干净利落

转换使用Val将ValueA转换为整数,并对ValueB进行相同的比较...

似乎有点太容易了...



格伦

(我不喜欢VB ...)

解决方案

如果你比较字符串,比较总是逐个字符,比较的结果总是基于找到的第一个不同字符对的结果。

所以,如果你com削减1与09999999999999第一个字符串将始终更大。

如果您需要数字比较,请始终转换为数值并进行比较。



BTW:你在玩VB有什么用?我虽然你讨厌它! :笑:

Hi,

A question if I need to compare two values (as strings) and use the StrComp() function in VB.Net as below:

ResultReadBackTest = StrComp(ValResultPastEquals, Value_Sx_Calculated_String)


What it does is checks the values read back from a device and compares it to what was sent.
The device only deals with strings.

However I can see issues with this method as I have lashed up a quick program to test

Dim ValueA As String
       Dim ValueB As String
       Dim Result As Integer


       ValueA = "0011059096"
       ValueB = "11059096"

       Result = StrComp(ValueA, ValueB)
       MsgBox("Result = " + Result.ToString())


The values I have got from the program I have written are 0011059096 and 11059096
which gives a 1, meaning from the MSDN page Value1 sorts after Value2 ??
I know if I did a substring and got rid of the leading zero's it might work but that could be dangerous. Opions please on the following:

intValA = Val(ValueA)
intValB = Val(ValueB)
MsgBox("Value A = " + intValA.ToString() + " Value B = " + intValB.ToString())
If (intValA = intValB) Then MsgBox("Huzzah")

or to put it cleanly
convert use Val to convert ValueA to an integer and the same for ValueB and do a compare...
Seems a little too easy...

Glenn
(I dislike VB...)

解决方案

If you compare strings, the comparison is always character-by-character, and the result of the comparison is always based on the result of the first different pair of characters found.
So if you compare "1" with "09999999999999" the first string will always be larger.
If you need a numeric comparison, always convert to numeric values and compare those.

BTW: What are yuou doing playing with VB? I though you detested it! :laugh:


这篇关于我这样做是正确的Strcomp值......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆