Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the GetChar() function

VB.Net program to demonstrate the GetChar() function

Write a VB.Net program to demonstrate the GetChar() function

The GetChar() function is used to get the characters in a given index from the string.

Syntax :

GetChar(str,index)

Parameters :

  • Str: This is the string specified.
  • Index: The index is used to get the character out of the string.

Return Value :

The Val() function returns the character in the specified index from the string.

Program :

Below is the source code for demonstrating the GetChar() function. The program given is compiled and successfully executed.

'VB.Net program to demonstrate the GetChar() function.


Module Module1
    Sub Main()
        Dim ch As Char
        Dim str1 As String = "Hello World"
        Dim str2 As String = "Hello India"
        Dim str3 As String = "Hello Delhi"
        ch = GetChar(str1, 5)
        Console.WriteLine("Char at index {0}: {1}", 5, ch)
        ch = GetChar(str2, 7)
        Console.WriteLine("Char at index {0}: {1}", 7, ch)
        ch = GetChar(str3, 8)
        Console.WriteLine("Char at index {0}: {1}", 8, ch)
    End Sub
End Module
	
Output:
Char at index 5: o
Char at index 7: I
Char at index 8: e

Explanation:

We created a Module1 module in the program above that contains the Main() method. Three variables str1, str2, and str3 were created in the Main() method, initialized with Hello World, Hello India, and Hello Delhi.

ch = GetChar(str1, 5)
Console.WriteLine("Char at index {0}: {1}", 5, ch)

ch = GetChar(str2, 7)
Console.WriteLine("Char at index {0}: {1}", 7, ch)

ch = GetChar(str3, 8)
Console.WriteLine("Char at index {0}: {1}", 8, ch)

In the above code, we obtained a character from a given string on a given index, and then printed it on the console screen.