比较 VB.NET 中的字符串 [英] Comparing strings in VB.NET

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

问题描述

希望这应该是一个简单的问题.在 Java 中,我认为它是 compareTo().

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

如何比较两个字符串变量以确定它们是否相同?

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

即:

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

推荐答案

我建议使用 String.Compare 方法.使用该方法,您还可以控制是否让它执行区分大小写的比较.

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.

示例:

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

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

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.NET 中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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