Using the Format Function with Custom String Formats

Level:
Level1

Written By TheVBProgramer.

$ReqTestHarness$

 

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.

If you enjoyed this post, subscribe for updates (it's free)

How do I put +/- sign in front of number

eg: -1.00, +1.00

thanks in advance

-1 => -1.00 1 => +1.00

-1 => -1.00
1 => +1.00

about this page

this is the best code to to learn. thank you.
regards from me.

Formatting NIFs

Hello, I want to format strings as follows:
- B26469117 --> B26.469.117
- 16525396B --> 16.525.396B
- X6615515W --> X6.615.515W

Is there anyway to do this width only one step?

What I have done is:
- Check if the string begins or ends width a letter.
- Format string with thousands without the letters (only digits). Format (strText ,"##,###") and then include the letters I have taken at the beginning or end of the string.
Is there any way to do it all in one step?

I'm awaiting your reply. Thanks.

Print "Using 'm/d/yy':";

Print "Using 'm/d/yy':"; Tab(30); Format$(Now, "m/d/yy")
Print "Using 'mm/dd/yyyy':"; Tab(30); Format$(Now, "mm/dd/yyyy")
Print "Using 'yyyy-mm-dd':"; Tab(30); Format$(Now, "yyyy-mm-dd")
Print "Using 'dddd, mmmm dd, yyyy':"; Tab(30); Format$(Now, "dddd, mmmm dd, yyyy")
Print "Using 'd-mmm':"; Tab(30); Format$(Now, "d-mmm")
Print "Using 'mmmm-yy':"; Tab(30); Format$(Now, "mmmm-yy")
Print "Using 'hh:mm AM/PM':"; Tab(30); Format$(Now, "hh:mm AM/PM")
Print "Using 'h:mm:ss a/p':"; Tab(30); Format$(Now, "h:mm:ss a/p")
Print "Using 'd-mmmm h:mm':"; Tab(30); Format$(Now, "d-mmmm h:mm")
Print "Using 'd-mmmm-yy':"; Tab(30); Format$(Now, "d-mmmm-yy")
Print "Using 'd mmmm':"; Tab(30); Format$(Now, "d mmmm")
Print "Using 'mmmm yy':"; Tab(30); Format$(Now, "mmmm yy")
Print "Using 'hh:mm AM/PM':"; Tab(30); Format$(Now, "hh:mm AM/PM")
Print "Using 'h:mm:ss a/p':"; Tab(30); Format$(Now, "h:mm:ss a/p")
Print "Using 'h:mm':"; Tab(30); Format$(Now, "h:mm")
Print "Using 'h:mm:ss':"; Tab(30); Format$(Now, "h:mm:ss")
Print "Using 'm/d/yy h:mm':"; Tab(30); Format$(Now, "m/d/yy h:mm")
End Sub

How i can use placeholder in

How i can use placeholder in this case:

"I have 1% apples and 2% biers"
to got this:
"I have 32 apples and 99 biers"

I'm building a multilingual application and some languages the sequence of words is not same.

Thanks in advance.

string = "I have %1% apples

string = "I have %1% apples and %2% biers" ' or its translated version
string = replace (string, "%1%", format (apples, "0"))
string = replace (string, "%2%", format (biers, "0"))