using the Right Function in Visual Basic .NET

maqskywalker
maqskywalker used Ask the Experts™
on
In VB.NET
I use the right function to get first character from the right like this.

Dim TestString As String = "HelloWorld"
VarCharacterString = Right(TestString, 1)

So  VarCharacterString contains "d"

Using the Right Function, How do i get the 5th character from the right?

So the character I want to save in a variable is the "W"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You need to be using SubString(), not Right().

To use SubString(), you pass in a starting position and a length (number of characters to return), so to get the 5th character from the right, you can work out the starting position by subtracting 5 from the string length:

Dim TestString As String = "HelloWorld"
VarCharacterString = TestString.Substring(TestString.Length - 5, 1)

Open in new window

Author

Commented:
Nice! Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial