A sub-string is a string that is created or retrieved
from an existing string. Once again, the Microsoft Visual Basic library
provides different techniques of creating it. These include getting
characters from the left, from the right, or from any part inside the
string.

The Left Sub-String

The Left() function can be used to get a number
of characters from the left side of a string. and create a new string The
syntax of this function is:

Left(string, length) As String

This function takes two required arguments. The first
argument is the string on which the operation will be performed. The
second argument, a constant integer, is the number of characters to
retrieve from the first argument. Here is an example:

As mentioned for the Left() function, you can
write the Right() function as Right$ to indicate that it
returns a string.

The Middle Sub-String

While the Left$() and the Right$()
functions work on both sides of a string, you may want to use characters
starting at any position of your choice to create a sub-string. This
operation is supported by the Mid() function. Its syntax is:

Mid(string, start[, length) As String

This function takes two required and one optional
arguments. The first argument, which is required, is the string on
which the operation will be carried. The second argument, also required,
is the position from where to start the sub-string inside the string
argument. Here is an example:

As you can see, if you omit the third argument, the
returning sub-string would start at the start position from the
string argument up to the end of the string. If you prefer, you
can create a sub-string that stops before the end. To do this, you can
pass the number of characters as the third argument. Here is an example:

If you have a string and want to find a character or a
sub-string in it, you can call the InStr() function. Its syntax is:

InStr([start, ]string1, string2[, compare])

The first argument specifies the position from the
string where to start looking for. This first argument is not required.
The second argument is required and specifies the string to examine. The
third argument specifies the character or the string to look for in the
second argument. The fourth argument, which is optional, specifies whether
the criterion would be binary or text-based.

The InStr() function works from the left side
of the considered string. If you want to find an occurrence of one or more
characters from the right side side of a string, you can use the
InStrRev() function instead.

Replacing Occurrences in a String

After finding a character or a sub-string in an
existing string, one of the operations you can perform would consist of
replacing that character or that sub-string with another character or a
sub-string. To do this, you can call the Replace() function. Its syntax
is:

The first argument to this function, expression,
is required. It holds the string that will be considered.

The second argument, find, also required, is
the character or the sub-string to look for in the expression.

The third argument also is required. It also is passed
as a string. If the find string is found in expression, it
would be replaced with the replace string.

When you call the Replace() function with the
three required arguments, it would proceed from the most left character of
the expression string. Instead of start with the first character,
you can specify another starting position of your choice within
expression. The fourth argument, which is optional, allows you to pass
this factor as a constant integer.

The fifth argument also is optional. If you omit it,
every time find would be found in expression, it would be
replaced with replace, as many times as possible. If you want, you
can limit the number of times this replacement should occur even if
find is found more than once. To specify this factor, pass the fifth
argument as a constant integer.

The compare argument, also optional, allows you
specify whether the comparison would be carried in text or binary format.
This argument can be passed as Text or Binary.

Replace the number with a decimal one such as 6829.283 and
click the button:

Replace the number with currency value such as $29.95 and
click the button:

Save and close the form

Introduction to Numeric Formatting

Formatting a Number in a String

Number formatting consists of specifying how a number
should display on a form or a report. To support it, the Visual Basic
language provides a function named Format. This function can be
used for different types of values The most basic technique consists of
passing it an expression that holds the value to display. The syntax of
this function is:

Besides the Format() function, the Visual Basic
language provides some additional functions. To control how the number
should display, you can pass the second argument of the Format()
function. This argument would be passed as a string.

A number is in general format if it doesn't use any
particular formtting. To use this format, pass the second argument to the
Format() function as "general number". This is case-insensitive.

The standard format is a combination of the general
and the fixed formats: The number must use the decimal separator and the
thousands separator, including the mechanisms to reconcile the digits on
both sides of the decimal separator. To display a number in the standard
format, you have various options. You can pass the second argument of the
Format() function as "standard" in
case-insensitive.

A percentage of a number represents its rate on a scale,
usually of 100 (or more). The number is expressed using digits accompanied
by the % sign.

To programmatically use a percentage number, you can use
the Format() function. Besides the Format() function, to
support percent values, the Visual Basic language provides a function named
FormatPercent. Its syntax is:

Only the first argument is required and it is the number
that needs to be formatted. When calling this function, pay attention to the
number you provide as argument. If the number represents a percentage value
as a fraction of 0 to 1, make sure you provide it as such. An example would
be 0.25. In this case, the Visual Basic interpreter would multiply the value
by 100 to give the result. Here is an example:

If you pass the value in the hundreds, the interpreter
would still multiply it by 100. Although it is not impossible to get a
percentage value in the hundreds or thousands, you should make sure that's
the type of value you mean to get.

Formatting a Number With Wild Cards

To let you further control how a numhber should display,
the second argument of the Format() function can include some special
characters. To represent the integral part of a number, use the # symbol.
You can enter as many # signs as you want. To specify the number of digits
to display on the right side of the decimal separator, type a period on the
right side of # followed by the number of 0s representing each decimal
place.

If you call the Format() function with the
Standard option, it would consider only the number of digits on the
right side of the decimal separator. If you want to display more digits than
the number actually has, call the FormatNumber() function and pass a
second argument with the desired number. Here is an example:

In the same way, if you want the number to display with
less numbers on the right side of the decimal separator, specify that
number.

You can call the Format() function to format the
number with many more options. To represent the integral part of a number,
you use the # sign. To specify the number of digits to display on the right
side of the decimal separator, type a period on the right side of # followed
by the number of 0s representing each decimal place. Here is an example:

The five 0s on the right side of the period indicate
that you want to display 5 digits on the right side of the period. You can
enter as many # signs as you want; it would not change anything. Here is an
example:

You can include any other character or symbol you want
in the string to be part of the result, but you should include such a
character only at the beginning or the end of the string, otherwise the
interpreter might give you an unexpected result.

Practical
Learning: Formatting a Number With Wild Cards

On the Ribbon, click File and click Open

In the listłof files, click Geometry1 from the previous lessons

In the Navigation Pane, right-click Hexagon and click Design View

In the Controls section of the Ribbon, click Image
and click the form

From the resources that accompany these lessons, select and open
Hexagon