The Format$ Function – Part 4
(Custom String Formats)
Custom string formatting can be accomplished using specific characters recognized by the Format$ function, shown in the table below:
< Force lowercase |
Display all characters in lowercase format. |
> Force uppercase |
Display all characters in uppercase format. |
@ Character placeholder |
Display a character or a space. If the string has a character in the position where the @ appears in the format string, display it; otherwise, display a space in that position. Placeholders are filled from right to left unless there is an ! character in the format string. |
& Character placeholder |
Display a character or nothing. If the string has a character in the position where the & appears, display it; otherwise, display nothing. Placeholders are filled from right to left unless there is an ! character in the format string. |
! Force left to right fill of placeholders |
The default is to fill from right to left. |
\ (Display the next character in the format string) |
Many characters in the format expression have a special meaning and can't be displayed as literal characters unless they are preceded by a backslash. The backslash itself isn't displayed. Using a backslash is the same as enclosing the next character in double quotation marks. To display a backslash, use two backslashes (\\). Examples of characters that can't be displayed as literal characters are the date- and time-formatting characters (a, c, d, h, m, n, p, q, s, t, w, y, and /:), the numeric-formatting characters (#, 0, %, E, e, comma, and period), and the string-formatting characters (@, &, <, >, and !).
|
"ABC" (Display the string inside the double quotation marks) |
To include a string in format from within code, you must enclose the text in quotation marks (to embed a quotation mark within a quoted string, use to consecutive quotation marks for the embedded quotation mark). |
To demonstrate custom string formats using combinations of the characters listed above, set up another "Try It" project, and place the following code in the cmdTryIt_Click event:
Private Sub cmdTryIt_Click()
Dim strName As String
strName = InputBox("Please enter your name:")
Print "Using '>':"; Tab(25); Format$(strName, ">")
Print "Using '<':"; Tab(25); Format$(strName, "<")
Print "Using '@':"; Tab(25); "Hello there, "; Format$(strName, "@@@@@@@@@@"); ". How are you?"
Print "Using '&':"; Tab(25); "Hello there, "; Format$(strName, "&&&&&&&&&&"); ". How are you?"
Print "Using '!@':"; Tab(25); "Hello there, "; Format$(strName, "!@@@@@@@@@@"); ". How are you?"
Print "Using '!&':"; Tab(25); "Hello there, "; Format$(strName, "!&&&&&&&&&&"); ". How are you?"
Print "Using '\':"; Tab(25); Format$(strName, "\H\e\l\l\o\,\ &&&&&&&&&&\.")
Print "Using embedded quotes:"; Tab(25); Format$(strName, """Hello, ""&&&&&&&&&&"".""")
End Sub
Run the project and click the "Try It" button. When the input box appears, enter a name in mixed case (in this example, Bruce was entered). The strings will be displayed as follows:
Download the VB project code for the example above here.