devxlogo

Using the Format Function With Strings

Using the Format Function With Strings

You’ll use the Format function most often with numbers, but it can be useful when applied to strings as well. For example, you can format a credit card number-which is held in a string variable, even if it contains only digits-and subdivide the number into four groups of four characters each, using a complex string expression:

 ' X holds the sequence of 16 digitsCreditCardNum = Left$(x, 4) & " " & Mid$(x, 5, 4) & " " & _	Mid$(x, 9, 4) & " " & Right$(x, 4)

The Format function lets you accomplish the same result in a more readable and efficient way:

 CreditCardNum = Format$(x, "!@@@@ @@@@ @@@@ @@@@")

devx-admin

Share the Post: