This tutorial shows many of the functions available in the cString.[method] set of functions.
These functions help to format, convert, and modifiy string values.
The cString object is another publicly available object that provides many methods
for dealing with strings. Some functions are enhanced versions of the VB counterpart,
while other functions are new and provide easy string manipulation.
Accessing the functions only requires calling the functions on the cString object.
Format items within a string can be replaced by values supplied to the cString.Format
command. Items are identified by enclosing the value indexes in curly braces.
Dim s As String
s = cString.Format("My name is {1}, {0} {1}.", "James", "Bond")
Debug.Print s
' output
' My name is Bond, James Bond.
Each format item corrisponds to a format value based on the index enclosed in braces.
The array of format values is zero-based.
Now that we have our slogan, lets add some highlighting by padding each end
with some cheverons.
s = cString.PadLeft(s, Len(s) + 3, ">")
s = cString.PadRight(s, Len(s) + 3, "<")
Debug.Print s
' output
' >>>My name is Bond, James Bond.<<<
This usage of padding is very similar to just prepending and appending 3 characters
by hand through string concatenation. A different usage would be when the total
width is calculated and the remaining space needs to be filled, such as for
aligning text.
Debug.Print "'"; cString.PadLeft("Kelly", 20, " ");"'"
' output
' ' Kelly'
This will right align any text to a 20 character width.
We don't like our cheverons added to our James Bond slogan. The easies thing
to do is trim them off. Unlike VB Trim functions, cString.Trim, cString.TrimStart,
and cString.TrimEnd allow for an array of characters to be supplied for removal.
s = cString.Trim(s, "<>")
Debug.Print s
' output
' My name is Bond, James Bond.
As you can see, both ends were trimmed of both '<' and '>' characters. If no
characters are supplied, then white-space is removed. This is just not spaces, but
also tabs, carriage returns, line-feeds and others. These trimming functions will
accept both a string of the characters and an Integer array of characters.
Converting a string to an Integer array equivalence with out any conversion
taking place, the cString.ToCharArray can be used.
Dim ch() As Integer
ch = cString.ToCharArray("hello")
Dim i As Long
For i = 0 To 4
Debug.Print "Char: "; ch(i)
Next i
' output
' Char: 104
' Char: 101
' Char: 108
' Char: 108
' Char: 111
cString provides methods for formatting, manipulating, and converting string
values. The most important functions are the cString.Format functions. To
learn more about the available functions review cString in Doc.
|