本文整理汇总了VB.NET中System.String.Contains方法的典型用法代码示例。如果您正苦于以下问题:VB.NET String.Contains方法的具体用法?VB.NET String.Contains怎么用?VB.NET String.Contains使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.String
的用法示例。
在下文中一共展示了String.Contains方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
Class Example
Public Shared Sub Main()
Dim s1 As String = "The quick brown fox jumps over the lazy dog"
Dim s2 As String = "fox"
Dim b As Boolean = s1.Contains(s2)
Console.WriteLine("'{0}' is in the string '{1}': {2}",
s2, s1, b)
If b Then
Dim index As Integer = s1.IndexOf(s2)
If index >= 0 Then
Console.WriteLine("'{0} begins at character position {1}",
s2, index + 1)
End If
End If
End Sub
End Class
输出:
fox' is in the string 'The quick brown fox jumps over the lazy dog': True fox begins at character position 17
示例2: StringExtensions
' 导入命名空间
Imports System.Runtime.CompilerServices
Module StringExtensions
<Extension()>
Public Function Contains(str As String, substring As String,
comp As StringComparison) As Boolean
If substring Is Nothing Then
Throw New ArgumentNullException("substring",
"substring cannot be null.")
Else If Not [Enum].IsDefined(GetType(StringComparison), comp)
Throw New ArgumentException("comp is not a member of StringComparison",
"comp")
End If
Return str.IndexOf(substring, comp) >= 0
End Function
End Module
示例3: Example
Public Module Example
Public Sub Main
Dim s As String = "This is a string."
Dim sub1 As String = "this"
Console.WriteLine("Does '{0}' contain '{1}'?", s, sub1)
Dim comp As StringComparison = StringComparison.Ordinal
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp))
comp = StringComparison.OrdinalIgnoreCase
Console.WriteLine(" {0:G}: {1}", comp, s.Contains(sub1, comp))
End Sub
End Module
输出:
Does 'This is a string.' contain 'this'? Ordinal: False OrdinalIgnoreCase: True