Word Document Properties


Listing 4-6 Finding Information in Strings by Using Parsing



Download 8,82 Mb.
Pdf ko'rish
bet87/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   83   84   85   86   87   88   89   90   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Listing 4-6
Finding Information in Strings by Using Parsing
Public Sub ParseString()
‘ Create a string with elements the program can parse.
Dim Mystr As String
‘ Create an output string.
Dim output As String
90
Part II: Learning the Ropes 
09_046500 ch04.qxp 12/5/06 5:54 PM Page 90


‘ Fill the input string with data.
Mystr = “A string to parse”
‘ Display the whole string.
output = “the whole string is: “ + Mystr
‘ Obtain the first word.
output = output + vbCrLf + “The First Word: “ + _
Left(Mystr, InStr(1, Mystr, “ “))
‘ Obtain the last word.
output = output + vbCrLf + “The Last Word: “ + _
Right(Mystr, Len(Mystr) - InStrRev(Mystr, “
“))
‘ Obtain the word string.
output = output + vbCrLf + “The Word String: “ + _
Trim(Mid(Mystr, _
InStr(1, Mystr, “string”), _
Len(Mystr) - InStr(1, Mystr,
“to”)))
‘Output the result.
MsgBox output, vbOKOnly, “parsing a String”
End Sub
The code begins by creating two variables: one to hold the input data and
a second to hold the output data. The input string has a simple phrase that
code can parse. The first parsing task is easy: Locate the first word in the
phrase. To perform this task, the 
Left
function retrieves text starting at
the first character and continuing until the first space. The 
InStr
function
returns the position of the first space as a number.
The second parsing task is more complicated. The 
Right
function returns
the right end of the string. The 
InStrRev
function returns the number of
characters from the beginning of the string to the last space. The 
Len
func-
tion returns a value of 17 (the length of the string), and 
InStrRev
returns 
12 (the number of characters from the beginning of the string to the last
space). The result is 5. Count the number of letters in 
Parse
(the last word in
the string) — it contains five characters. The 
Right
function returns 
Parse
in this case.
The third parsing task is to look for the word 
String
in 
MyStr
. The example
code shows that you can perform nesting as needed. The nesting begins
with the 
Trim
function, which removes any extra characters. The 
Mid
func-
tion requires three arguments for this example. The first is the string that
you want to work with. The second is the starting position. Notice that the
code uses the 
InStr
function, but it searches for 
String
and not a space this
time. The third is the length of the string. The length of the string is still 17,
91

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   83   84   85   86   87   88   89   90   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish