BlackOpty BlackOpty - 1 month ago 5
Vb.net Question

Get the first character of string using VB.NET

Let’s say that my string is:

test!


I want to get the first character

t est!

Also I want to get the last character

test
!


How can I do that?

Answer

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)
Comments