String Functions
These are a few string functions that would be useful to create consistent
input from user input, to work with strings in program code and to prepare
output for forms or print. This is a partial list.
|
|
Use Asc and Chr to get an ASCII code from a character, and to
get a character from an ASCII code. VB 2005 also includes
a variation of these commands to work with Unicode; AscW and
ChrW.
Be sure to test for for Null before using these.
Asc
Returns an integer - the ASCII code for the character.
Syntax:
intNew = Asc(variable)
intNew is the integer variable that will recieve the result
of the function.
Variable is the string variable to convert from a character to an
ASCII code. It is assumed this variable contains a single
character. If there are more than one characters, only the
ASCII code for the first character will be returned. A
null variable will cause an error.
| Example:
Dim codeInt As Integer
' The following line of code sets myInt to 65.
codeInt = Asc("A")
' The following line of code sets myInt to 97.
codeInt = Asc("a")
|
Chr
Returns a character - the character the ASCII code
represents..
Syntax:
strNew = chr(variable)
strNew is the string variable that will recieve the result of
the function.
Variable is the integer variable to convert from an ASCII
code to a string character. A
null variable will cause an error.
Example:Dim associatedChar As Char
' Returns "A".
associatedChar = Chr(65)
' Returns "a".
associatedChar = Chr(97)
' Returns ">".
associatedChar = Chr(62)
' Returns "%".
associatedChar = Chr(37)
|
|
|
|
Formats strings for output on the form or to the printer.
Syntax:
NewStr = Format(Variable, format name)
NewStr is the new variable that receive the result of the
method.
Variable is the variable to be formatted.
Format name is taken from the following tables. It is
enclosed in double quotes.
| Example: ' Returns current system
date in the system-defined long date format.
TestStr = Format(Now(), "Long Date")
|
Formats for Numbers
|
| Format name |
Description |
| General Number, G, or g |
Displays number with no thousand separator. |
| Currency, C, or c |
Displays number with thousand separator, if appropriate;
displays two digits to the right of the decimal separator.
Output is based on system locale settings. |
| Fixed, F, or f |
Displays at least one digit to the left and two digits
to the right of the decimal separator. |
| Standard, N, or n |
Displays number with thousand separator, at least one
digit to the left and two digits to the right of the decimal
separator. |
| Percent |
Displays number multiplied by 100 with a percent sign
(%) appended immediately to the right; always displays two
digits to the right of the decimal separator. |
| P, or p |
Displays number with thousandths separator multiplied by
100 with a percent sign (%) appended to the right and
separated by a single space; always displays two digits to
the right of the decimal separator. |
| Scientific |
Uses standard scientific notation, providing two
significant digits. |
| E, or e |
Uses standard scientific notation, providing six
significant digits. |
| D, or d |
Displays number as a string that contains the value of
the number in Decimal (base 10) format. This option is
supported for integral types (Byte, Short,
Integer, Long) only. |
| X, or x |
Displays number as a string that contains the value of
the number in Hexadecimal (base 16) format. This option is
supported for integral types (Byte, Short,
Integer, Long) only. |
| Yes/No |
Displays No if number is 0; otherwise,
displays Yes. |
| True/False |
Displays False if number is 0; otherwise,
displays True. |
| On/Off |
Displays Off if number is 0; otherwise,
displays On. |
Formats for Dates
|
| Format Name |
Description |
| General Date, or G |
Displays a date and/or time. For example, 4/3/93
05:34 PM. Date display is determined by your system's
LocaleID value. |
| Long Date, Medium Date, or D |
Displays a date according to your locale's long date
format. |
| Short Date, or d |
Displays a date using your locale's short date format. |
| Long Time, Medium Time, or T |
Displays a time using your locale's long time format;
typically includes hours, minutes, seconds. |
| Short Time, or t |
Displays a time using your locale's short time format. |
| f |
Displays the long date and short time according to your
locale's format. |
| F |
Displays the long date and long time according to your
locale's format. |
| g |
Displays the short date and short time according to your
locale's format. |
| M, m |
Displays the month and the day of a date. |
| R, r |
Formats the date and time as Greenwich Mean Time (GMT). |
| s |
Formats the date and time as a sortable index. |
| u |
Formats the date and time as a GMT sortable index. |
| U |
Formats the date and time with the long date and long
time as GMT. |
| Y, y |
Formats the date as the year and month. |
|
|
|
Convert to lowercase or uppercase. Syntax:
strNew = LCase(variable)
strNew is the string variable to receive the output of the method.
Variable is the string variable to convert.
Example:
strGreeting = "Hello VB Programmer"
strLCGreeting = LCase(strGreeting)
' Returns "hello vb programmer"
strUCGreeting = UCase(strGreeting)
' Returns "HELLO VB PROGRAMMER"
|
|
|
|
Find the length of a string. Returns the length of the string as an integer. Syntax:
intNew = Len(variable)
intNew is the integer variable to receive the output of the
method.
Variable is the string variable to count the number of characters
in..
' Initializes variable.
Dim TestString As String = "Hello Programmer
' Returns 16.
Dim TestLen As Integer = Len(TestString)
|
|
|
|
Converts its numeric argument to a string with the
numeric digits in the string. Syntax:
strNew = str(variable)
strNew is the new string variable.
variable is the numeric variable that gets converted.
|
|
|
Left(str, int)
Returns the far-left int characters from
the argument.
Len(str)
Returns the number of characters in the string.
(Notice that Len() works on numeric arguments as well.)
LTrim(str)
Returns the string argument, without leading
spaces.
Mid(str, intStart [, intLen])
Returns a substring of the argument, starting with
the character at intStart and continuing until the entire
the string is extracted or until the optional intLen
characters have been extracted. Mid() is called the
midstring function because it can return the middle portion of a
string.
Right(str, int)
Returns the far-right int characters from
the argument.
RTrim(str)
Returns the far-right int characters from
the argument. |
|
|
Compare two strings.
Syntax:
intNew = StrComp(variable1, variable2,
compare method)
intNew recieves the result. It will be an integer; -1,
0 or 1.
The compare methods are CompareMethod.Text and CompareMethod.Binary
CompareMethod.Text will compare upper and lower case equally.
Binary will see they are different.
| Example:
' Define variables.
Dim TestStr1 As String = "ABCD"
Dim TestStr2 As String = "abcd"
Dim TestComp As Integer
' The two strings sort equally. Returns 0.
TestComp = StrComp(TestStr1, TestStr2, CompareMethod.Text)
' TestStr1 sorts after TestStr2. Returns -1.
TestComp = StrComp(TestStr1, TestStr2, CompareMethod.Binary)
' TestStr2 sorts before TestStr1. Returns 1.
TestComp = StrComp(TestStr2, TestStr1) |
| If |
StrComp returns |
| String1
sorts ahead of String2 |
-1 |
| String1
is equal to String2 |
0 |
| String1
sorts after String2 |
1 |
|
|
|
Use to convert strings to all lower, all upper or title case.. Syntax:
strNew = StrConv(variable, conversion type)
strNew is the variable that will recieve the result of the
function.
Variable is the name of the string variable to be converted.
Conversion type is VbStrConv.UpperCase, VbStrConv.LowerCase
or VbStrConv.ProperCase,
| Example:
StrLCaseName = "My NaMe"
StrLCaseName=
StrConv(strName, VbStrConv.LowerCase)
'returns "my name"
StrLowerCaseName is assigned the value the strConv function
returns. It changes to lowercase all the characters in the
variable strName.
|
Conversion Types
|
| VbStrConv.UpperCase |
Converts the string to uppercase characters. |
| VbStrConv.LowerCase |
Converts the string to lowercase characters. |
| VbStrConv.ProperCase |
Converts the first letter of every word in string to
uppercase. |
|
|