BlackOpty BlackOpty - 4 months ago 17 Question

Get the first character of string using VB.NET

Let’s say that my string is:


I want to get the first character

t est!

Also I want to get the last character


How can I do that?


The easiest is using LINQ:

Dim firstChar = str.First()
Dim lastChar  = str.Last()

You can use LINQ since a String is also an IEnumerable(Of Char) implicitely.

You can also use it like a Char-Array(String.Chars is the default property) and access the first and last char via index:

firstChar = str(0)
lastChar  = str(str.Length - 1)