String.Contains Method (String)
Note |
|---|
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience. |
Returns a value indicating whether a specified substring occurs within this string.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
-
Type:
System.String
The string to seek.
Return Value
Type: System.Booleantrue if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException | value is null. |
This method performs an ordinal (case-sensitive and culture-insensitive) comparison. The search begins at the first character position of this string and continues through the last character position.
To determine whether a string contains a specified substring by using something other than ordinal comparison (such as culture-sensitive comparison, or ordinal case-insensitive comparison), you can create a custom method. The following example illustrates one such approach. It defines a String extension method that includes a StringComparison parameter and indicates whether a string contains a substring when using the specified form of string comparison.
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
The following example then calls the Contains extension method to determine whether a substring is found in a string when using ordinal comparison and case-insensitive ordinal comparison.
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 ' The example displays the following output: ' Does 'This is a string.' contain 'this'? ' Ordinal: False ' OrdinalIgnoreCase: True
If you are interested in the position of the substring value in the current instance, you can call the IndexOf method to get the starting position of its first occurrence, or you can call the LastIndexOf method to get the starting position of its last occurrence. The example includes a call to the IndexOf(String) method if a substring is found in a string instance.
The following example determines whether the string "fox" is a substring of a familiar quotation. If "fox" is found in the string, it also displays its starting position.
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 ' ' This example displays the following output: ' 'fox' is in the string 'The quick brown fox jumps over the lazy dog': True ' 'fox begins at character position 17
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
