string 比较vb中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/900927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
comparing strings in vb
提问by Fredrik M?rk
Hopefully this should be an easy question. In java i think it's compareTo()
.
希望这应该是一个简单的问题。在 Java 中,我认为它是compareTo()
.
How do I compare 2 string variables to determine if they are the same?
如何比较 2 个字符串变量以确定它们是否相同?
ie:
IE:
If (string1 = string2 And string3 = string4) Then
'perform operation
Else
'perform another operation
End If
回答by Fredrik M?rk
I would suggest using the String.Comparemethod. Using that method you can also control whether to to have it perform case-sensitive comparisons or not.
我建议使用String.Compare方法。使用该方法,您还可以控制是否让它执行区分大小写的比较。
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 StringComparisonparameter:
编辑:如果要执行不区分大小写的搜索,可以使用StringComparison参数:
If String.Compare(str1, str2, StringComparison.InvariantCultureIgnoreCase) = 0 And String.Compare(str3, str4, StringComparison.InvariantCultureIgnoreCase) = 0 Then
回答by Robert Harvey
Dim MyString As String = "Hello World"
Dim YourString As String = "Hello World"
Console.WriteLine(String.Equals(MyString, YourString))
returns a bool True. This comparison is case-sensitive.
返回一个布尔值 True。这种比较区分大小写。
So in your example,
所以在你的例子中,
if String.Equals(string1, string2) and String.Equals(string3, string4) then
' do something
else
' do something else
end if
回答by Tim
In vb.net you can actually compare strings with =
. Even though String
is a reference type, in vb.net =
on String
has been redefined to do a case-sensitive comparison of contentsof the two strings.
在 vb.net 中,您实际上可以将字符串与=
. 即使String
是引用类型,在vb.net=
上String
也被重新定义为对两个字符串的内容做区分大小写的比较。
You can test this with the following code. Note that I have taken one of the values from user input to ensure that the compiler cannot use the same reference for the two variables like the Java compiler would if variables were defined from the same string Literal. Run the program, type "This" and press <Enter>.
您可以使用以下代码对此进行测试。请注意,我从用户输入中获取了其中一个值,以确保编译器不能像 Java 编译器那样对两个变量使用相同的引用,如果变量是从相同的字符串 Literal 定义的。运行程序,输入“This”并按<Enter>。
Sub Main()
Dim a As String = New String("This")
Dim b As String
b = Console.ReadLine()
If a = b Then
Console.WriteLine("They are equal")
Else
Console.WriteLine("Not equal")
End If
Console.ReadLine()
End Sub
回答by illuminati_confirmed
I know this has been answered, but in VB.net above 2013 (the lowest I've personally used) you can just compare strings with an =
operator. This is the easiest way.
我知道这已经得到了回答,但是在 2013 年以上的 VB.net(我个人使用过的最低版本)中,您可以将字符串与=
运算符进行比较。这是最简单的方法。
So basically:
所以基本上:
If string1 = string2 Then
'do a thing
End If
回答by Aman
I think this String.Equals is what you need.
我认为这个 String.Equals 是你需要的。
Dim aaa = "12/31"
Dim a = String.Equals(aaa, "06/30")
a will return false.
a 将返回 false。
回答by Sreenath Munireddy
If String.Compare(string1,string2,True) Then
'perform operation
EndIf