QTP&VBS编程讨论,如何取得字符串中的页码
比如 “page 4 of 1118”,我们想取得这串字符中的数字4作为当前页,1118作为总的页码;请注意,页码的位数是不定的,而且字符串中的间隔不一定规范,比如4of15这种也有可能遇到。使用vbs拿到这两个数字?请给出你的解决方法。这个是QTP unplugged上的题目,看似简单,其实是不简单的,呵呵。 我给出我的解决方法:'How can we find the current page and the total page values from the string "Page 4 of 15"?
Option Explicit
Dim strText
strText = "Page 4 of 15"
'msgbox Mid(strText, 6, 1)
'Msgbox Right(strText, 2)
Dim strCurrPage, strTotPage
GetCurrTotPage strText, strCurrPage, strTotPage
msgbox "Current Page is " & strCurrpage & "!" & vbCrLf& _
"Total Page is " & strTotPage & "!"
Public Function GetCurrTotPage(ByVal strText, ByRef strCurrPage, ByRef strTotPage)
'Use Regular Expression
Dim rePattern, reObj
rePattern = "\d+\s*of\s*\d+"
Set reObj = New RegExp
reObj.Pattern = rePattern
reObj.Global = False
reObj.IgnoreCase = True
'msgbox reObj.Test(strText)
If reObj.Test(strText) Then
Dim oCollMatches
Set oCollMatches = reObj.Execute(strText)
If oCollMatches.Count > 0 Then
Dim strMatch, strMatchValue
For Each strMatch in oCollMatches
strMatchValue = strMatch.Value
strCurrPage = Trim(Mid(strMatchValue, 1, InStr(strMatchValue, "of") - 1))
strTotPage = Trim(Mid(strMatchValue, InStr(strMatchValue, "of") + 2))
Next
End If
GetCurrTotPage = True
Else
msgbox "Please pass the valid string into function!"
End If
End Function
页:
[1]