How to VB.NET String.Contains()
The Contains() method is a built-in function of the String class in VB.NET. It's used to determine if a specific substring (smaller string) exists within a given source string. It essentially performs a search operation to check for the presence of the substring.
System.String.Contains(String str) As Boolean
Parameters: - String str - input String for search
- Boolean - Yes/No.
Functionality
- It takes a single argument, which is the substring you want to search for within the source string.
- The search is performed in a case-sensitive manner by default. This means that "Hello" and "hello" would be considered different substrings.
- If the entire substring is found anywhere within the source string, the method returns True.
- Conversely, if the substring is not found or if the source string is empty, the method returns False.
Dim sourceString As String = "This is a sample string."
Dim subString As String = "sample"
Dim isContained As Boolean = sourceString.Contains(subString) ' True
In this example:
- sourceString holds the string to be searched.
- subString holds the substring to be searched for.
- isContained is a Boolean variable that will store the result (True if found, False otherwise).
System.ArgumentNullException :If the argument is null.
Key Points: - Case Sensitivity: By default, Contains() performs a case-sensitive search. To achieve a case-insensitive search, you can convert either the source string or the substring to lowercase before using Contains().
- Partial Matches: The method only checks for the exact presence of the entire substring. It doesn't identify occurrences where the substring forms part of a larger word within the source string (e.g., "sample" wouldn't be found in "sampling").
Dim message As String = "Welcome to VB.NET!"
Dim searchTerm As String = "vb.net" ' Lowercase search term
Dim isFound As Boolean = message.ToLower().Contains(searchTerm.ToLower()) ' True (case-insensitive)
Full Source VB.NET
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
str = "VB.NET TOP 10 BOOKS"
If str.Contains("TOP") = True Then
MsgBox("The string Contains() 'TOP' ")
Else
MsgBox("The String does not Contains() 'TOP'")
End If
End Sub
End Class
Common Use Cases: - Validating user input: You can use Contains() to check if a user-entered value includes a specific term or pattern.
- Searching text content: It's helpful for tasks like searching for keywords within documents or code.
- String manipulation: Combined with other string methods, Contains() can be used for various string processing tasks.
Conclusion
The VB.NET String.Contains() method is used to determine whether a specified substring is present within a given string, returning a Boolean value indicating its existence or absence.
Related Topics
- How to vb.net String Length()
- How to vb.net String Insert()
- How to vb.net String IndexOf()
- How to vb.net String Format()
- How to vb.net String Equals()
- How to vb.net String CopyTo()
- How to vb.net String Copy()
- How to vb.net String Compare()
- How to vb.net String Clone()
- How to vb.net String Chars()
- How to vb.net String substring()
- How to vb.net String Split()
- How to vb.net String EndsWith()
- How to vb.net String Concat()
- How to VB.NET String Null
Related Links