Strings & String Manipulation in Visual Basic .Net

The collection of characters are called strings.

Declaring String in VB.Net

Syntax

Dim string_name As String

The string can be initialized at the time of string declaration.

Dim string_name As String = "String Text"

String Class Properties

Chars : To return the Char object at a specified position in the current String object.

Length : To return the number of characters in the current String object.

String Class Methods

Compare :

Public Shared Function Compare ( str1 As String, str2 As String ) As Integer

To compare two specified strings and returns the result to an integer variable.

Concat :

Public Shared Function Concat ( str0 As String, str1 As String ) As String

To concatenate two specified string objects.

Contains :

Public Function Contains ( value As String ) As Boolean

Returns a Boolean value whether a specified string contains within this string.

Copy :

Public Shared Function Copy ( str As String ) As String

Creates a new string object with the same value of the specified string.

CopyTo :

Public Sub CopyTo ( sourceIndex As Integer, destination As Char(), destinationIndex As Integer, count As Integer )

Copies a specified number of characters from a specified position of the string object to a specified position in an array of Unicode characters.

EndsWith :

Public Function EndsWith ( value As String ) As Boolean

To return whether the specified string part matches with the end of the string or not.

Equals :

Public Function Equals ( value As String ) As Boolean

To return whether the specified string matches with the given string or not.

Public Shared Function Equals ( a As String, b As String ) As Boolean

To return whether two specified strings have the same value.

Format :

Public Shared Function Format ( format As String, arg0 As Object ) As String

Replaces one or more format items in a specified string with the string representation of a specified object.

IndexOf :

Public Function IndexOf ( value As Char ) As Integer

To return the index of very first occurrence of a given character in a specified string.

Insert :

Public Function Insert ( startIndex As Integer, value As String ) As String

To return a new string after inserting a specified string to a given string.

IsNullOrEmpty :

Public Shared Function IsNullOrEmpty ( value As String ) As Boolean

To return a Boolean value, by checking whether the specified string is null or empty or not.

Join :

Public Shared Function Join ( separator As String, ParamArray value As String() ) As String

Concatenates all the elements of a string array, using the specified separator between each element.

LastIndexOf :

Public Function LastIndexOf ( value As Char ) As Integer

To return an integer by searching a String in reverse for the index.

Remove :

Public Function Remove ( startIndex As Integer ) As String

Remove all the elements of the string from a specified position.

Replace :

Public Function Replace ( oldChar As Char, newChar As Char ) As String

Replace the specified char from a string with a given char.

ToLower :

Public Function ToLower As String

Returns a new string after converting the given string to Lower case.

ToUpper :

Public Function ToUpper As String

Returns a new string after converting the given string to Upper case.

Other Methods:

  • Split
  • StartsWith
  • ToCharArray
  • Trim

Example Program

Module Module1

Sub Main()
 Dim str1, str2 As String
 Dim op As Integer
 Console.WriteLine("Select Your operation (Enter the number)")
 Console.WriteLine("1. Compare")
 Console.WriteLine("2. ToLower")
 Console.WriteLine("3. Contains")
 Console.WriteLine("4. ToUpper")
 Console.WriteLine("5. Join")
 Console.WriteLine("6. Substring")
 Console.WriteLine("7. Insert")
 Console.WriteLine("8. Equals")
 op = Console.ReadLine()
 Select Case op
 Case 1
 Console.WriteLine("Enter string 1")
 str1 = Console.ReadLine()
 Console.WriteLine("Enter a string 2")
 str2 = Console.ReadLine()
 If (String.Compare(str1, str2) = 0) Then
 Console.WriteLine(str1 + " and " + str2 + " are equl ")
 Else
 Console.WriteLine(str1 + " and " + str2 + " are not equl ")
 End If

Case 2
 Console.WriteLine("Enter a string")
 str1 = Console.ReadLine()
 str1 = str1.ToLower()
 Console.WriteLine(str1)
 Case 3
 Console.WriteLine("Enter the string")
 str1 = Console.ReadLine()
 console.WriteLine("Enter the string to check contain: ")
 str2 = console.ReadLine()
 If (str1.contains(str2)) Then
 console.Writeline(str2 + "was found in" + str1)
 End If
 Case 4
 Console.WriteLine("Enter the string")
 str1 = Console.ReadLine()
 str1 = str1.ToUpper()
 Console.WriteLine(str1)
 Case 5
 Console.WriteLine("Enter the string 1: ")
 str1 = Console.ReadLine()
 Console.WriteLine("Enter the string 2: ")
 str2 = Console.ReadLine()
 Dim str3 As String = String.Join(" ", str1, str2)
 console.WriteLine("The joined string is: " + str3)
 Case 6
 Console.WriteLine("Enter the string")
 str1 = Console.ReadLine()
 Console.WriteLine("Enter the index for substring: ")
 Dim ind As Integer = Console.ReadLine()
 str2 = str1.Substring(ind)
 Console.WriteLine(str2)
 Case 7
 Console.WriteLine("Enter the string: ")
 str1 = Console.ReadLine()
 Console.WriteLine("Enter the position to insert the string: ")
 Dim ind As Integer = Console.ReadLine()
 Console.WriteLine("Enter the string to insert: ")
 str2 = Console.ReadLine()
 str1 = str1.Insert(ind, str2)
 Console.WriteLine("The string after insertion is: " + str1)
 str1 = Console.ReadLine()
 Case 8
 Console.WriteLine("Enter the string 1: ")
 str1 = Console.ReadLine()
 Console.WriteLine("Enter the string 2: ")
 str2 = Console.ReadLine()
 If (String.Equals(str1, str2)) Then
 Console.WriteLine(str1 + "and" + str2 + "are equal")
 Else
 Console.WriteLine(str1 + "and" + str2 + "are not equal")
 End If

Case Else
 Console.WriteLine("!!!...ERROR...!!! INVALID SELECTION")
 End Select

Console.ReadKey()
 End Sub

End Module