本文整理匯總了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