BlackOpty BlackOpty - 1 year ago 84 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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download