All Categories :
ActiveX
Chapter 7
Manipulating Strings
CONTENTS
String manipulation is important for data verification and formatting
strings to your specifications prior to submitting data to the
server. For example, you can ensure that all text strings are
uppercase to stay in line with your database design.
VBScript gives you a rich set of functions to help you manipulate
strings and characters. All the string functions are very easy
to use, which means that you can add complex string checking and
formatting to your Web pages in just a few lines of code.
You can also use VBScript's string-manipulation functions to parse
a complete string quickly and easily. For example, you can split
a phrase such as "This is a string" into its individual
words. Using a similar routine, you can search for a particular
string within another string, as you saw in the last chapter where
you validated an e-mail address by searching for the @
character.
This chapter looks deeper into the syntax of the most widely used
string functions. A complete list of string functions is available
in Appendix C.
Note |
The sample in this chapter was designed so that you can add as much or as little of its functionality as you want. First, build the sampler template, and then you can add various sections. You can use each task in this chapter within the sampler template as a stand-alone page if you want. Add the form definitions inside the <TABLE> tags and the subprocedures inside the <SCRIPT> tags.
|
You can use your favorite HTML editor, Notepad, or even the text
editor of the ActiveX Control Pad to write the HTML and script
code. This sample doesn't use any ActiveX controls, so you don't
need to use the ActiveX Control Pad to produce the script sections.
The HTML code for the example document is shown here:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="vbscript">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="white">
<CENTER>
<H2>String Manipulation</H2>
<TABLE BORDER=1>
</TABLE>
</CENTER>
</BODY>
</HTML>
After you build your template, save it as strings.htm.
You're ready to start adding some string-manipulation functions.
VBScript has two functions for converting a string to either all
uppercase or all lowercase letters. Non-alphabetical characters
within the string remain untouched. First, look at the function
to change the string to lowercase.
The lowercase and uppercase data entry for the sample application
is held within the same form. Enter a string in the text box text1,
and click either the Make Uppercase or Make Lowercase button.
The result appears in the Output text box. The HTML code for the
sample form is as follows:
<FORM NAME="frmUprLwr">
<TR><TD>
Enter a string <INPUT TYPE="text" NAME="Text1">
</TD><TD>
<INPUT TYPE="button" NAME="MakeLowerCase" VALUE="Lower Case">
<INPUT TYPE="button" NAME="MakeUpperCase" VALUE="Upper Case">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
To convert a string to lowercase, you use the LCase function.
LCase takes only one variable-the string that you want
to convert. LCase returns a string of lowercase characters.
Only the characters A to Z are converted; all other characters
(such as @,!, <, and so on) remain
in their normal state.
Enter the following event handler for the MakeLowerCase
button's OnClick event in the <SCRIPT>
tags of the sampler template:
2: Sub MakeLowerCase_OnClick
3: Document.frmUprLwr.Output.Value = LCase(Document.frmUprLwr.Text1.Value)
4: End Sub
Conversion to uppercase or lowercase is useful if you have to
check a particular alphabetical-character word or even a phrase
that the user entered. Suppose you have a text box that enables
the user to enter a word such as "Admin." You check
to see whether the word was entered in order to invoke a further
process, but here's your problem: Did the user type "Admin,"
"admin," or even "AdMiN"? Character strings
are literal and "AdMiN" does not equal "admin."
A solution is to convert the string entered by the user to an
uppercase "ADMIN" and then check to see whether it matches
"ADMIN."
The VBScript function for converting a string to uppercase is
UCase, and it takes just one variable, the string you
want to convert. To add uppercase conversion functionality to
the sampler, enter the following event handler for the MakeUpperCase
button's OnClick event in the <SCRIPT>
tags of the sampler template:
5: Sub MakeUpperCase_OnClick
6: Document.frmUprLwr.Output.Value = UCase(Document.frmUprLwr.Text1.Value)
7: End Sub
The American Standard Code for Information Interchange (ASCII)
seven-bit character set represents letters and symbols found on
a standard U.S. keyboard. The ASCII character set is the same
as the first 128 characters (0-127) in the ANSI character set.
The American National Standards Institute (ANSI) eight-bit character
set, which is used by Microsoft Windows, enables you to represent
up to 256 characters (0-255). The first 128 characters (0-127)
correspond to the letters and symbols on a standard U.S. keyboard.
The second 128 characters (128-255) represent special characters,
such as letters in international alphabets, currency symbols,
and fractions.
ASCII and ANSI codes are the means by which the computer stores
alphabetical and other characters by converting the character
to a number, as detailed previously. You can access these numbers
to perform special string manipulations. For example, you can
simulate the UCase and LCase functions by performing
a simple calculation on the individual string characters. (Uppercase
characters are always the lowercase character's ASCII code minus
32.) You also can create simple encryption methods by subjecting
each character in the string to a calculation.
With the many built-in string-manipulation functions available
to you in VBScript, the usefulness of having direct access to
ASCII/ANSI codes is starting to diminish. To demonstrate the codes
and their string representations, the following sample page automatically
prints every character in the set from 32 to 255:
<HTML>
<SCRIPT LANGUAGE="vbscript">
Document.Write "<BODY BGCOLOR='white'>"
For x = 32 to 255
Document.Write CStr(x) & " " & Chr(x) & "<BR>"
next
Document.Write "</BODY>"
</SCRIPT>
</HTML>
The ANSI/ASCII characters from 0 to 32 are either system calls,
nondisplaying characters, or nonsupported characters. The most
common use for the lower numbers are codes 10 and 13, which correspond
to the line feed and carriage return. Note that several Windows
controls, such as the message box, can display true carriage returns
only by embedding a line feed followed by a carriage return, as
in the following example:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="vbscript">
Sub ShowMessage_OnClick
Dim strMessage
Dim CRLF
CRLF = Chr(10) & Chr(13)
strMessage = "This is the first line" & CRLF
strMessage = strMessage & "This is the second line"
x = MsgBox(strMessage,0,"My Message")
End Sub
</SCRIPT>
</HEAD>
<BODY BGCOLOR="white">
<CENTER>
<INPUT TYPE="button" NAME="ShowMessage" VALUE="Click Me">
</CENTER>
</BODY>
</HTML>
Enter the following HTML form to the sample template within the
<TABLE> tags:
<FORM NAME="frmAscChr">
<TR><TD>
Enter a Number <INPUT TYPE="text" NAME="Text1" SIZE=10>
</TD><TD>
<INPUT TYPE="button" NAME="MakeString" VALUE="Convert to a String">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
The conversion function from ASCII code to string, Chr(),
takes only one variable, the ASCII code, which must be a number.
First, the procedure checks to ensure that the value entered in
the text1 text box can be converted to a number. If it
cannot, a warning message displays to the user and the subroutine
terminates. This verification is usually not necessary because,
most of the time, you generate the ASCII code from within the
script, rather than obtaining it from a form input.
If the verification is successful, a second check makes sure the
number is within ASCII limits. Here, I've restricted the lower
limit to 32 because some of the lower numbers are used for system
functions such as beeping.
Finally, the conversion itself takes place. Because an HTML form
can return only string data, the value contained in Text1
must be converted to an integer prior to being used as an ASCII
code. Without this conversion, a Type Mismatch runtime
error occurs. The following code shows how to validate the entry
and convert the value within MakeString's OnClick
event:
8: Sub MakeString_OnClick
9: If Not IsNumeric(Document.frmAscChr.Text1.Value) Then
10: Alert "Only Numbers are allowed"
11: Exit Sub
12: End if
13: If CInt(Document.frmAscChr.Text1.Value) > 255 OR _
14: CInt(Document.frmAscChr.Text1.Value) < 32 Then
15: Alert "Must be between 32 and 255"
16: Exit Sub
17: End If
18: Document.frmAscChr.Output.Value = Chr(CInt(Document.frmAscChr.Text1.Value))
19: End Sub
Add the preceding subroutine to your sample HTML template between
the <SCRIPT> tags.
For finding the numeric representation of a character in the ANSI/ASCII
character set, VBScript provides the Asc() function,
which is the reverse of the Chr() function.
Simply add the following lines of HTML between the <TABLE>
tags to include the form that calls the sample event handler:
<FORM NAME="frmChrAsc">
<TR><TD>
Enter a Character <INPUT TYPE="text" NAME="Text1" SIZE=10>
</TD><TD>
<INPUT TYPE="button" NAME="MakeNumber" VALUE="Convert to a Number">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
Converting from string data to ASCII code couldn't be much simpler;
the Asc() function accepts a string character and returns
the ASCII code. You can give the function a full string such as
"What Is This"; however, it converts only the
first character of the string to an ASCII code.
The data retrieved from an HTML form is always string data (even
if the user has typed in a number), and the function accepts string
data, so there is not much point in verifying that which cannot
be wrong! That's why the procedure is a simple one-liner.
Type the following lines between the <SCRIPT> tags
of the template, and you've got instant ASCII convertibility:
20: Sub MakeNumber_OnClick
21: Document.frmChrAsc.Output.Value = Asc(Document.frmChrAsc.Text1.Value)
22: End Sub
Determining the length of a string is seldom very useful on its
own. However, when you combine this function with the other string-manipulation
functions, you can perform almost any string formatting and checking
you might need and exercise real power over your character strings.
(See Listing 7.1.) In fact, many string operations wouldn't even
be possible without determining the length of the string or the
length of part of a string.
Listing 7.1. Adding the form to the sample page.
<FORM NAME="frmLen">
<TR><TD>
Enter a String <INPUT TYPE="text" NAME="Text1">
</TD><TD>
<INPUT TYPE="button" NAME="GetLength" VALUE="Find String Length">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
You determine the length of a string using the VBScript Len()
function, which receives one variable: the string to be sized.
Len returns an integer that is the number of characters
in the string.
No data-type checking is involved in this particular procedure,
so you can add it as you use it within a script:
23: Sub GetLength_OnClick
24: Document.frmLen.Output.Value = Len(Document.frmLen.Text1.Value)
25: End Sub
Note |
The length of a string includes any leading or trailing spaces that the user entered. For example, if you enter " string " (four spaces, six characters, and four spaces without the quotation marks) into the text box, the Len() function returns 13. To clear leading spaces, use LTrim(string); to clear trailing spaces, use RTrim(string). To clear both leading and trailing spaces, use Trim(string) as follows:
newstring = LTrim(oldstring)
newstring = RTrim(oldstring)
newstring = Trim(oldstring)
|
Being able to access particular parts of a string is an important
part of string manipulation. Take a look at the function that
returns a given number of characters starting with the leftmost
character.
The following code segment shows the HTML code that you can add
between the <TABLE> tags on the sampler page to
use the left function example:
<FORM NAME="frmLeft">
<TR><TD>
Enter a String <INPUT TYPE="text" NAME="Text1">
</TD><TD>
x<INPUT TYPE="text" NAME="LeftChars" SIZE=10>
<INPUT TYPE="button" NAME="GetLeft" VALUE="Get Left x Chars">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
To return the leftmost character or characters of a string, use
the Left() function. The syntax for using the Left
function is
strVariable = Left(SourceString, n)
n is the number of characters from the leftmost character
you need to return. See the previous note regarding leading spaces
in the string, because they can seriously affect the resulting
string, giving you unpredictable results.
The number of left characters you specify cannot be a negative
number. If the number specified is greater than the length of
the string, the whole string is returned.
The following code shows the Left() function at work
in the sample application. Add this code between the <SCRIPT>
tags of your strings sampler:
26: Sub GetLeft_OnClick
27: If Not IsNumeric(Document.frmLeft.LeftChars.Value) Then
28: Alert "Only Numbers are allowed in the x Box"
29: Exit Sub
30: Else
31: intNoLeft = CInt(Document.frmLeft.LeftChars.Value)
32: End if
33: If intNoLeft < 0 Then
34: Alert "You cannot use a negative value"
35: Exit Sub
36: End if
37: Document.frmLeft.Output.Value = Left(Document.frmLeft.Text1.Value,
intNoLeft)
38: End Sub
The procedure first validates that the entry is numeric; if it
is, the entry is then converted to an integer. The integer is
checked to ensure that it is positive or 0. The result of the
Left function is displayed in the form's Output text
box.
To complement the Left function, Microsoft also provides
you with a Right() function, which returns the rightmost
n characters of a string.
The following code segment shows the HTML code that you can add
between the <TABLE> tags on the sampler page to
use the Right function example:
<FORM NAME="frmRight">
<TR><TD>
Enter a String <INPUT TYPE="text" NAME="Text1">
</TD><TD>
x<INPUT TYPE="text" NAME="RightChars" SIZE=10>
<INPUT TYPE="button" NAME="GetRight" VALUE="Get Right x Chars">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
To return the rightmost character or characters of a string, use
the Right() function. The syntax for using the Right
function is
strVariable = Right(SourceString, n)
n is the number of characters up to the rightmost character
you want to return. See the previous note regarding leading spaces
in the string, because they can seriously affect the resulting
string, giving you unpredictable results. n cannot be
a negative number. If the number specified is greater than the
length of the string, the whole string is returned.
The following code shows the Right() function in the
sample application. Add this code between the <SCRIPT>
tags of your strings sampler:
39: Sub GetRight_OnClick
40: If Not IsNumeric(Document.frmRight.RightChars.Value) Then
41: Alert "Only Numbers are allowed in the x Box"
42: Exit Sub
43: Else
44: intNoRight = CInt(Document.frmRight.RightChars.Value)
45: End if
46: If intNoRight < 0 Then
47: Alert "You cannot use a negative value"
48: Exit Sub
49: End if
50: Document.frmRight.Output.Value = Right(Document.frmRight.Text1.Value,
intNoRight)
51: End Sub
You saw how to return the right section of a string and how to
return the left section of a string, but what about a section
of the string that sits somewhere in the middle?
Microsoft thought about that one, too. The function is called
Mid(), and you can have some serious fun with it. If
you use Mid() in conjunction with the other string-manipulation
functions such as Len() and InStr() (which you'll
see later in this chapter), you can even build a mini parsing
tool.
Add the following code between the <TABLE> tags
in the sampler template:
<FORM NAME="frmMid">
<TR><TD>
Enter String <INPUT TYPE="text" NAME="Text1"><BR>
Enter Mid String Length <INPUT TYPE="text" NAME="Text2" SIZE=6>
</TD><TD>
Start at <INPUT TYPE="text" NAME="MidChars" SIZE=6>
<INPUT TYPE="button" NAME="GetMid" VALUE="Get Mid String">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
The Mid() function has two different syntaxes. The first
is
strVariable = Mid(SourceString, intStart)
This first syntax returns a string starting with the character
located at intStart and ending with the last character
of the string. This method is similar to using Right();
the subtle difference is that you can use Mid() without
knowing how many characters come after the start character position.
The second syntax is
strVariable = Mid(SourceString, intStart, intLength)
The second syntax returns a string of length intLength
starting with the character located at intStart. Both
intStart and intLength can be other scripted
manipulation functions. In both syntax cases, intStart
cannot be less than 1.
To demonstrate Mid(), type the following code (without
the line numbers) between the <SCRIPT> tags in
the strings sampler page:
52: Sub GetMid_OnClick
53: If Not IsNumeric(Document.frmMid.MidChars.Value) OR _
54: Not IsNumeric(Document.frmMid.Text2.Value)Then
55: Alert "Only Numbers are allowed"
56: Exit Sub
57: Else
58: intStart = CInt(Document.frmMid.MidChars.Value)
59: intMidStrLen = CInt(Document.frmMid.Text2.Value)
60: End if
61: If intStart > Len(Document.frmMid.Text1.Value) OR intStart < 1 Then
62: Alert "Start Number must be less than the string length and greater than 0"
63: Exit Sub
64: End if
65: strSource = Document.frmMid.Text1.Value
66: Document.frmMid.Output.Value = Mid(strSource, intStart, intMidStrLen)
67: End Sub
Suppose that you need to know whether a string contains a given
character or series of characters and, if it does, where in the
string the character or string of characters begins. This is probably
the most common string-manipulation problem on the face of the
earth. "I know it's probably in there, but it might not be;
if it isn't, it should be, and if it is, I want it on its own."
VBScript's InStr() function comes to the rescue. InStr()
finds one string within another string and tells you where the
string is located. You can also tell InStr where to start
looking, which is a particularly important feature. Suppose that
you have a fairly long string that might contain several instances
of the string you're looking for. If you couldn't tell InStr
where to start looking, you'd find only the first instance, but
by giving InStr a starting point, you can find the first
instance, jump past it to find the second, and so on.
Include the following HTML code in the sampler page table to try
the string search function:
<FORM NAME="frmInStr">
<TR><TD>
Enter Source String <INPUT TYPE="text" NAME="Text1"><BR>
Enter Search String <INPUT TYPE="text" NAME="Text2" SIZE=10>
</TD><TD>
Start at <INPUT TYPE="text" NAME="StartChars" SIZE=6>
<INPUT TYPE="button" NAME="GetInStr" VALUE="Find String">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
InStr() takes three variables and returns a string. The
syntax for InStr() follows:
strVariable = InStr(intStart, strSource, strSearch)
The starting position for the search (intStart) cannot
be less than 1 or greater than the length of the string. If InStr
finds the search string within the source string, it returns the
starting position of the string. If it doesn't find the string,
it returns zero.
To try InStr, add the following code to your sampler
application. Enter a string, a search string, and the starting
position for the search.
68: Sub GetinStr_OnClick
69: If Not IsNumeric(Document.frmInStr.StartChars.Value) Then
70: Alert "Start must be numeric"
71: Exit Sub
72: Else
73: intStart = CInt(Document.frmInStr.StartChars.Value)
74: End if
75: If intStart > Len(Document.frmInStr.Text1.Value) OR intStart < 1 Then
76: Alert "Start Number must be less than the string length and greater than 0"
77: Exit Sub
78: End if
79: strSource = Document.frmInStr.Text1.Value
80: strSearch = Document.frmInStr.Text2.Value
81: Document.frmInStr.Output.Value = InStr(intStart, strSource, strSearch)
82: End Sub
Figure 7.1 shows what the final string-manipulation sampler pages
look like in the browser.
Figure 7.1 : The string-manipulation sampler.
Listing 7.2 contains the complete source code for the page.
Listing 7.2. The strings.htm
code.
<HTML>
<HEAD>
1: <SCRIPT LANGUAGE="vbscript">
2: Sub MakeLowerCase_OnClick
3: Document.frmUprLwr.Output.Value = LCase(Document.frmUprLwr.Text1.Value)
4: End Sub
5: Sub MakeUpperCase_OnClick
6: Document.frmUprLwr.Output.Value = UCase(Document.frmUprLwr.Text1.Value)
7: End Sub
8: Sub MakeString_OnClick
9: If Not IsNumeric(Document.frmAscChr.Text1.Value) Then
10: Alert "Only Numbers are allowed"
11: Exit Sub
12: End if
13: If CInt(Document.frmAscChr.Text1.Value) > 255 OR _
14: CInt(Document.frmAscChr.Text1.Value) < 20 Then
15: Alert "Must be between 20 and 255"
16: Exit Sub
17: End If
18: Document.frmAscChr.Output.Value = Chr(CInt(Document.frmAscChr.Text1.Value))
19: End Sub
20: Sub MakeNumber_OnClick
21: Document.frmChrAsc.Output.Value = Asc(Document.frmChrAsc.Text1.Value)
22: End Sub
23: Sub GetLength_OnClick
24: Document.frmLen.Output.Value = Len(Document.frmLen.Text1.Value)
25: End Sub
26: Sub GetLeft_OnClick
27: If Not IsNumeric(Document.frmLeft.LeftChars.Value) Then
28: Alert "Only Numbers are allowed in the x Box"
29: Exit Sub
30: Else
31: intNoLeft = Cint(Document.frmLeft.LeftChars.Value)
32: End if
33: If intNoLeft < 0 Then
34: Alert "You cannot use a negative value"
35: Exit Sub
36: End if
37: Document.frmLeft.Output.Value = Left(Document.frmLeft.Text1.Value,
intNoLeft)
38: End Sub
39: Sub GetRight_OnClick
40: If Not IsNumeric(Document.frmRight.RightChars.Value) Then
41: Alert "Only Numbers are allowed in the x Box"
42: Exit Sub
43: Else
44: intNoRight = CInt(Document.frmRight.RightChars.Value)
45: End if
46: If intNoRight < 0 Then
47: Alert "You cannot use a negative value"
48: Exit Sub
49: End if
50: Document.frmRight.Output.Value = Right(Document.frmRight.Text1.Value,
intNoRight)
51: End Sub
52: Sub GetMid_OnClick
53: If Not IsNumeric(Document.frmMid.MidChars.Value) OR _
54: Not IsNumeric(Document.frmMid.Text2.Value)Then
55: Alert "Only Numbers are allowed"
56: Exit Sub
57: Else
58: intStart = CInt(Document.frmMid.MidChars.Value)
59: intMidStrLen = CInt(Document.frmMid.Text2.Value)
60: End if
61: If intStart > Len(Document.frmMid.Text1.Value) OR intStart < 1 Then
62: Alert "Start Number must be less than the string length and
greater than 0"
63: Exit Sub
64: End if
65: strSource = Document.frmMid.Text1.Value
66: Document.frmMid.Output.Value = Mid(strSource, intStart, intMidStrLen)
67: End Sub
68: Sub GetinStr_OnClick
69: If Not IsNumeric(Document.frmInStr.StartChars.Value) Then
70: Alert "Start must be numeric"
71: Exit Sub
72: Else
73: intStart = CInt(Document.frmInStr.StartChars.Value)
74: End if
75: If intStart > Len(Document.frmInStr.Text1.Value) OR intStart < 1 Then
76: Alert "Start Number must be less than the string length and
greater than 0"
77: Exit Sub
78: End if
79: strSource = Document.frmInStr.Text1.Value
80: strSearch = Document.frmInStr.Text2.Value
81: Document.frmInStr.Output.Value = InStr(intStart, strSource, strSearch)
82: End Sub
83: </SCRIPT>
</HEAD>
<BODY BGCOLOR="white">
<CENTER>
<H2>String Manipulation</H2>
<TABLE BORDER=1>
<FORM NAME="frmUprLwr">
<TR><TD>
Enter a string <INPUT TYPE="text" NAME="Text1">
</TD><TD>
<INPUT TYPE="button" NAME="MakeLowerCase" VALUE="Lower Case">
<INPUT TYPE="button" NAME="MakeUpperCase" VALUE="Upper Case">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TR>
</FORM>
<FORM NAME="frmAscChr">
<TR><TD>
Enter a Number <INPUT TYPE="text" NAME="Text1" SIZE=10>
</TD><TD>
<INPUT TYPE="button" NAME="MakeString" VALUE="Convert to a String">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
<FORM NAME="frmChrAsc">
<TR><TD>
Enter a Character <INPUT TYPE="text" NAME="Text1" SIZE=10>
</TD><TD>
<INPUT TYPE="button" NAME="MakeNumber" VALUE="Convert to a Number">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
<FORM NAME="frmLen">
<TR><TD>
Enter a String <INPUT TYPE="text" NAME="Text1">
</TD><TD>
<INPUT TYPE="button" NAME="GetLength" VALUE="Find String Length">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
<FORM NAME="frmLeft">
<TR><TD>
Enter a String <INPUT TYPE="text" NAME="Text1">
</TD><TD>
x<INPUT TYPE="text" NAME="LeftChars" SIZE=10>
<INPUT TYPE="button" NAME="GetLeft" VALUE="Get Left x Chars">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
<FORM NAME="frmRight">
<TR><TD>
Enter a String <INPUT TYPE="text" NAME="Text1">
</TD><TD>
x<INPUT TYPE="text" NAME="RightChars" SIZE=10>
<INPUT TYPE="button" NAME="GetRight" VALUE="Get Right x Chars">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
<FORM NAME="frmMid">
<TR><TD>
Enter String <INPUT TYPE="text" NAME="Text1"><BR>
Enter Mid String Length <INPUT TYPE="text" NAME="Text2" SIZE=6>
</TD><TD>
Start at <INPUT TYPE="text" NAME="MidChars" SIZE=6>
<INPUT TYPE="button" NAME="GetMid" VALUE="Get Mid String">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
<FORM NAME="frmInStr">
<TR><TD>
Enter Source String <INPUT TYPE="text" NAME="Text1"><BR>
Enter Search String <INPUT TYPE="text" NAME="Text2" SIZE=10>
</TD><TD>
Start at <INPUT TYPE="text" NAME="StartChars" SIZE=6>
<INPUT TYPE="button" NAME="GetInStr" VALUE="Find String">
</TD><TD>
<INPUT TYPE="text" NAME="Output">
</TD></TR>
</FORM>
</TABLE>
</BODY>
</HTML>
In this chapter, you saw all the major string-manipulation functions
at work and built a sampler page that will prove useful for experimenting
with your own applications.
The following list outlines a summary of the string functions
you saw in this chapter:
- To convert a string to all uppercase characters, use UCase("String").
- To convert a string to all lowercase characters, use LCase("String").
- To convert from ASCII codes to strings, use Chr(x).
- To convert from strings to ASCII codes, use Asc("String").
- To return the leftmost x characters, use Left("String",x).
- To return the rightmost x characters, use Right("String",x).
- To return any part of the string, use Mid("String",Start,Length).
- To find one string within another, use InStr(Start,"Source","Search").
- To remove leading spaces, use LTrim("String").
- To remove trailing spaces, use RTrim("String").
- To remove leading and trailing spaces at the same time, use
Trim("String").
Now that you've seen some fairly advanced and sophisticated techniques
for manipulating string values, have a look at the following chapters:
- To learn more about using strings with arrays, see Chapter
10, "Using the Power of Arrays."
- Chapter 19, "Baking Cookies with VBScript," discusses
using string manipulation within a cookie file.
Q: | Is there a built-in VBScript function that can be used to format strings in a certain way, like the Visual Basic Format() function?
|
A: | Unfortunately, due to size constraints, Microsoft left the Format() function out of the first release of VBScript. However, using a combination of the techniques you have seen in this chapter, you can replicate the functionality of Format(). It is rumored that later releases of VBScript will include a scaled-down version of Format().
|