String.Contains (), compare, clone in VB.net

By: Issac Viewed: 153361 times  Printer Friendly Format    


Contains method is an important string class method which checks whether specified parameter string exists in the string or not

System.String.Contains(String str) As Boolean 

Parameters: 
String str – the input String which we gonna search

Returns: 
Boolean - Yes/No
If the str is in the String then it returns true
If the str is not in the String it returns False

For ex: "I am what I am".Contains ("I") returns True
"This is a Test”.Contains (“me”) returns False

Exceptions:
System.ArgumentNullException : If the argument is null

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 = "for java articles java-samples.com"
If str.Contains("samples") = True Then
MsgBox("The string Contains() ‘samples’ ")
Else
MsgBox("The String does not contains() ’samples’ ")
End If
End Sub
End Class

When you run the program you will get the MessageBox with message "The string Contains() 'samples' "

The VB.NET String Compare function is used to compare two String Objects.
System.String.Compare(String str1,String str2, Boolean ) As Integer 
It returns an Integer indication which specifies the logical relationship between the two strings

Parameters: 
• String str1 
• String str2 
Boolean True/False Indication is to check whether the String is case sensitive or not

Returns: 
Integer : returns less than zero, zero or greater than zero.
Less than zero : str1 is less than str2
zero : str1 is equal to str2
Greater than zero : str1 is grater than str2

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim str1 As String
Dim str2 As String

str1 = "issac"
str2 = "ISSAC"

Dim result As Integer

result = String.Compare(str1, str2)
MsgBox(result)

result = String.Compare(str1, str2, True)
MsgBox(result)
End Sub
End Class

When you run this code first u get -1 in message box specifying string 2 is greater and then 0 specifying the strings are equal.

The VB.NET String Clone () method returns a reference to this instance of String.
Public Function Clone() As Object 

Returns: 
Object : Return the instance of the String
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String = "Clone() Test"
Dim clonedString As String
clonedString = str.Clone()
MsgBox(clonedString)
End Sub
End Class


When you run this program you ill get the same content of the first string "Clone() Test"



Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial