比较vb中的字符串 [英] comparing strings in vb

查看:135
本文介绍了比较vb中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这应该是一个容易的问题。在java中我认为它是 compareTo()



如何比较2个字符串变量,相同?



ie:

  If(string1 = string2 And string3 = string4)然后
'执行操作
否则
执行另一个操作
结束如果


解决方案

我建议使用 String.Compare 方法。



示例:



pre> Dim str1 As String =String one
Dim str2 As String = str1
Dim str3 As String =String three
Dim str4 As String = str3

如果String.Compare(str1,str2)= 0和String.Compare(str3,str4)= 0然后
MessageBox.Show(str1 = str2 And str3 = str4 )
Else
MessageBox.Show(Else)
结束如果


$ b b

编辑:如果要执行不区分大小写的搜索,可以使用 StringComparison 参数:

 如果String.Compare(str1,str2,StringComparison.InvariantCultureIgnoreCase)= 0和String.Compare(str3,str4,StringComparison.InvariantCultureIgnoreCase)= 0然后


Hopefully this should be an easy question. In java i think it's compareTo().

How do I compare 2 string variables to determine if they are the same?

ie:

If (string1 = string2 And string3 = string4) Then
    'perform operation
Else
    'perform another operation
End If

解决方案

I would suggest using the String.Compare method. Using that method you can also control whether to to have it perform case-sensitive comparisons or not.

Sample:

Dim str1 As String = "String one"
Dim str2 As String = str1
Dim str3 As String = "String three"
Dim str4 As String = str3

If String.Compare(str1, str2) = 0 And String.Compare(str3, str4) = 0 Then
    MessageBox.Show("str1 = str2 And str3 = str4")
Else
    MessageBox.Show("Else")
End If

Edit: if you want to perform a case-insensitive search you can use the StringComparison parameter:

If String.Compare(str1, str2, StringComparison.InvariantCultureIgnoreCase) = 0 And String.Compare(str3, str4, StringComparison.InvariantCultureIgnoreCase) = 0 Then

这篇关于比较vb中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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