Get last 4 (Four) characters of a string using C# and VB.Net example

Last Reply on Oct 20, 2015 02:13 AM By Shashikant

Posted on Oct 20, 2015 02:13 AM

Hello all,

I am working on getting the last 4 digits of a vin number. i thought it would be something simple like this. but i am getting outofrange exception errors.

 

            /** Last 4 digits of VIN # **/
            string wholeVIN = record.vehicle.vehicleidentificationnumber;
            string lastFourOfVIN = wholeVIN.Substring(wholeVIN.Length - 4, wholeVIN.Length-1);

 

Posted on Oct 20, 2015 02:13 AM Modified on on Oct 23, 2015 02:48 AM

Hi,

Please try below code

C#

string wholeVIN = "Ass5125ss8uhh";
string lastFourOfVIN = wholeVIN.Substring(wholeVIN.Length - 4, 4);

OR

string wholeVIN = "Ass5125ss8uhh";
string lastFourOfVIN = wholeVIN.Substring(wholeVIN.Length - 4, wholeVIN.Length - (wholeVIN.Length - 4));

VB

Dim wholeVIN As String = "Ass5125ss8uhh"
Dim lastFourOfVIN As String = wholeVIN.Substring(wholeVIN.Length - 4, 4)

OR

Dim wholeVIN As String = "Ass5125ss8uhh"
Dim lastFourOfVIN As String = wholeVIN.Substring(wholeVIN.Length - 4, wholeVIN.Length - (wholeVIN.Length - 4))

I hope this will help you out.