v_kns v_kns - 3 months ago 8
Vb.net Question

Get the first character of a string, to get initial from name

I have to shorten the length of a string variable to one character. The problem arises as I don't know the length of the variable.

The code I have written has been designed to take a first name and surname and display them in a label in the format of SURNAME.INITIAL.

How would I be able to shorten the length of the variable FirstName to one letter?

Answer

Say you have the string Mary. To get the M you can do this:

Dim str As String
str = "Mary"

Dim first_char As Char
first_char = str .Substring(0, 1)

first_char will have the M