Let’s say you have a string of data from a credit card that you swiped using a credit card swiper device. In the standard formatted string of data that is received into your string variable from the credit card, you want to extract the credit card number and expiration date in order to populate a windows form.
The following demonstration walks you through this scenario using class methods provided to you by Microsoft’s DLL libraries.
Let’s say you have a string of data from the card swiper that looks like this:
CreditCardData = 1234567890123456^JOHN DOE^1206XXXXXXXXXXXXXXXXXXXXXXXXXX The first 16 characters is the credit card number.
The name is JOHN DOE between the two carrots ‘^’.
The expiration year is 12 and the month is 06 after the second carrot ‘^’.
First, let’s find the starting position of the first carrot ‘^’ so we can grab the credit card number. The credit card number is prior to the first carrot ‘^’.
We can use the Strings.InStr method:
Dim mFindCarrotBeforeName As Integer = Strings.InStr(_CreditCardData, "^") mFindCarrotBeforeName value should be 17.
Now we know how to get the credit card number. Using mFindCarrotBeforeName, we can use the Strings.Left method.
Dim mHoldCreditCardNumber As String = Strings.Left(_CreditCardData, (mFindCarrotBeforeName - 1))
The credit card number should be everything starting at the left of the string for a length of 16 (mFindCarrotFirstCarrot – 1= 16)
Now to simplify things, let’s remove everything from the start of the string up to the name. The following statement results in removing the credit card number plus the first carrot using the mFindCarrotBeforeName integer value of 17.
Dim mRemoveUpToName As String = _ CreditCardData.Remove(1, mFindCarrotBeforeName)
mRemoveUpToName should now contain the following:
Now we know how to get past the name to determine the starting position of our expiration year and month. Using mRemoveUpToName, we can use the Strings.Instr method to get the first carrot ‘^’.
Dim mFindCarrotAfterName As Integer = Strings.InStr(mRemoveUpToName, "^")
mFindCarrotAfterName value should be 9.
Now we know how to get the expiration year and month. Using mFindCarrotAfterName, we can use the Strings.Mid method.
Dim mYearMonth As String = Strings.Mid(mRemoveUpToName, (mFindCarrotAfterName + 1), 4)
This statement says, using the mRemoveUpToName string,
Starting at position 10 (mFindCarrotAfterName + 1 = 10), get me the 4 character expiration year and month.
mYearMonth should now = 1206
Lastly, I want to reverse the order of the Year and Month. Using again the Strings.Mid and Strings.Left, I can accomplish this in the following statement:
Dim mHoldExpirationDate As String = (Strings.Mid(mYearMonth, 3, 2)) & (Strings.Left(mYearMonth, 2))
mHoldExpirationDate now equals 0612.