Programming Tutorials

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

By: Issac in VB.net Tutorials on 2009-01-30  

In VB.net, the String.Contains() method is used to determine whether a specified string exists within another string. It returns a boolean value indicating whether the search string was found or not.

Here's an example:

Dim str As String = "Hello World"
Dim searchStr As String = "World"
If str.Contains(searchStr) Then
    Console.WriteLine("The search string was found in the main string.")
Else
    Console.WriteLine("The search string was not found in the main string.")
End If

The String.Compare() method in VB.net is used to compare two strings and determine whether they are equal or not. It returns an integer value indicating the result of the comparison.

Here's an example:

Dim str1 As String = "Hello"
Dim str2 As String = "World"
Dim result As Integer = String.Compare(str1, str2)
If result = 0 Then
    Console.WriteLine("The two strings are equal.")
ElseIf result < 0 Then
    Console.WriteLine("The first string is less than the second string.")
ElseIf result > 0 Then
    Console.WriteLine("The first string is greater than the second string.")
End If

The String.Clone() method in VB.net is used to create a new copy of the string object. It returns an object that is a copy of the original string.

Here's an example:

Dim str1 As String = "Hello World"
Dim str2 As String = str1.Clone()
Console.WriteLine("Original string: " & str1)
Console.WriteLine("Cloned string: " & str2)

The String.CopyTo() method in VB.net is used to copy a section of the string object to a character array. It takes two arguments: the index position in the string where the copy operation should start, and the character array that will receive the copied characters.

Here's an example:

Dim str As String = "Hello World"
Dim charArray(4) As Char
str.CopyTo(6, charArray, 0, 5)
Console.WriteLine("Copied characters: " & New String(charArray))

Note that the first argument is the index position in the string where the copy operation should start, and the second argument is the character array that will receive the copied characters. The third argument is the index position in the character array where the copied characters should be placed, and the fourth argument is the number of characters to copy. In this example, the method copies 5 characters starting at index position 6 in the string object to the character array.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)