Vb.net 使用通配符比较两个字符串
我想比较两个字符串。我想说s1等于s2,因为常用的单词“hello”。有没有关于如何在有或没有其他功能的情况下实现这一点的建议Vb.net 使用通配符比较两个字符串,vb.net,Vb.net,我想比较两个字符串。我想说s1等于s2,因为常用的单词“hello”。有没有关于如何在有或没有其他功能的情况下实现这一点的建议 s1: hello s2: hello world if s1 = s2 then ..do something else ..do something end if 听起来好像要比较子字符串,可以使用String.Contains: Dim s1 = "hello" Dim s2 = "hello world" Dim s2Conta
s1: hello
s2: hello world
if s1 = s2 then
..do something
else
..do something
end if
听起来好像要比较子字符串,可以使用
String.Contains
:
Dim s1 = "hello"
Dim s2 = "hello world"
Dim s2ContainsHello As Boolean = s2.Contains(s1) ' True
或者(如果要忽略大小写)String.IndexOf
,如果找不到,则返回-1:
s2 = "Hello World"
s2ContainsHello = s2.IndexOf(s1, StringComparison.InvariantCultureIgnoreCase) >= 0 ' Still True
VB.NET中的第三个选项是使用Like
操作符(默认情况下也区分大小写):
支持通配符:
Characters in pattern Matches in string
? Any single character
* Zero or more characters
# Any single digit (0–9)
[charlist] Any single character in charlist
[!charlist] Any single character not in charlist
听起来好像要比较子字符串,可以使用
String.Contains
:
Dim s1 = "hello"
Dim s2 = "hello world"
Dim s2ContainsHello As Boolean = s2.Contains(s1) ' True
或者(如果要忽略大小写)String.IndexOf
,如果找不到,则返回-1:
s2 = "Hello World"
s2ContainsHello = s2.IndexOf(s1, StringComparison.InvariantCultureIgnoreCase) >= 0 ' Still True
VB.NET中的第三个选项是使用Like
操作符(默认情况下也区分大小写):
支持通配符:
Characters in pattern Matches in string
? Any single character
* Zero or more characters
# Any single digit (0–9)
[charlist] Any single character in charlist
[!charlist] Any single character not in charlist
如果你只是想知道两个字符串中是否都有“hello”
If s1.Contains("hello") AndAlso s2.Contains("hello") Then
'do something
Else
'do something else
End If
如果你只是想知道两个字符串中是否都有“hello”
If s1.Contains("hello") AndAlso s2.Contains("hello") Then
'do something
Else
'do something else
End If
这很不清楚。如果他们都有字母
e
?还是一个空格?没关系,只要里面有“你好”这个词,我想说两者是相等的。所以打电话给.Contains(“你好”)
?你比我慢了很多,这很不清楚。如果他们都有字母e
?还是一个空格?没关系,只要里面有“hello”这个词,我想说两者是相等的。所以打电话给.Contains(“hello”)
?你比我慢