String Manipulation
VBscript has several string related function which make searching,extracting
and concatenating string data easy. The following examples uses the
mid, len and trim functions to extract each word and print it on
a separate line.
<html>
<head>
<title>String Manipulation Example</title>
</head>
<body>
<%
Dim strText,intSpace, strWord, intCounter
intSpace =1
strText="the quick brown fox jumped over the lazy dog"
For intCounter=1 to len(strText)+1
if (Mid(strText,intCounter,1) = " " or intCounter=len(strText)+1)
then
strWord=mid(strText,intSpace,intCounter-intSpace)
intSpace=intCounter
strWord=trim(strWord)
%>
<p><b><%=strWord%>
<%End if
Next
%>
</p>
</b>
</body>
</html>
How is works:
The program searches for spaces in strText and when it finds them,
extracts the word which is in between the spaces and prints out the
word.
Right Function
This example uses the Right function to return a specified
number of characters from the right side of a string.
Dim AnyString, MyStr
AnyString = "Hello World" ' Define string.
MyStr = Right(AnyString, 1) ' Returns "d".
MyStr = Right(AnyString, 6) ' Returns " World".
MyStr = Right(AnyString, 20) ' Returns "Hello World".
Left Function
This example uses the Left function to return a specified
number of characters from the left side of a string.
Dim AnyString, MyStr
AnyString = "Hello World" ' Define string.
MyStr = Left(AnyString, 1) ' Returns "H".
MyStr = Left(AnyString, 7) ' Returns "Hello W".
MyStr = Left(AnyString, 20) ' Returns "Hello World".
LTrim, RTrim, and Trim Functions
Returns a Variant (String)
containing a copy of a specified string without leading spaces (LTrim),
trailing spaces (RTrim), or both leading and trailing spaces
(Trim).
Syntax
LTrim(string)
RTrim(string)
Trim(string)
Len Function
The first example uses Len to return the number of characters
in a string or the number of bytes required to store a variable.
The Type...End Type block defining CustomerRecord must
be preceded by the keyword Private if it appears in a class
module. In a standard module, a Type statement can be Public.
Type CustomerRecord ' Define user-defined type.
ID As Integer ' Place this definition in a
Name As String * 10 ' standard module.
Address As String * 30
End Type
Dim Customer As CustomerRecord ' Declare variables.
Dim MyInt As Integer, MyCur As Currency
Dim MyString, MyLen
MyString = "Hello World" ' Initialize variable.
MyLen = Len(MyInt) ' Returns 2.
MyLen = Len(Customer) ' Returns 42.
MyLen = Len(MyString) ' Returns 11.
MyLen = Len(MyCur) ' Returns 8.
The second example uses LenB and a user-defined function
(LenMbcs) to return the number of byte characters in a string
if ANSI is used to represent the string.
Function LenMbcs (ByVal str as String)
LenMbcs = LenB(StrConv(str, vbFromUnicode))
End Function
Dim MyString, MyLen
MyString = "ABc"
' Where "A" and "B" are DBCS and "c" is SBCS.
MyLen = Len(MyString)
' Returns 3 - 3 characters in the string.
MyLen = LenB(MyString)
' Returns 6 - 6 bytes used for Unicode.
MyLen = LenMbcs(MyString)
' Returns 5 - 5 bytes used for ANSI.
|