51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1552|回复: 1
打印 上一主题 下一主题

[原创] QTP&VBS编程讨论,如何取得字符串中的页码

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2011-10-13 22:33:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
比如 “page 4 of 1118”,我们想取得这串字符中的数字4作为当前页,1118作为总的页码;

请注意,页码的位数是不定的,而且字符串中的间隔不一定规范,比如4of15这种也有可能遇到。使用vbs拿到这两个数字?请给出你的解决方法。这个是QTP unplugged上的题目,看似简单,其实是不简单的,呵呵。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2011-10-13 22:33:41 | 只看该作者
我给出我的解决方法:
  1. 'How can we find the current page and the total page values from the string "Page 4 of 15"?
  2. Option Explicit
  3. Dim strText
  4. strText = "Page 4 of 15"
  5. 'msgbox Mid(strText, 6, 1)
  6. 'Msgbox Right(strText, 2)
  7. Dim strCurrPage, strTotPage
  8. GetCurrTotPage strText, strCurrPage, strTotPage
  9. msgbox "Current Page is " & strCurrpage & "!" & vbCrLf  & _
  10.        "Total Page is " & strTotPage & "!"
  11. Public Function GetCurrTotPage(ByVal strText, ByRef strCurrPage, ByRef strTotPage)
  12.    'Use Regular Expression
  13.    Dim   rePattern, reObj
  14.          rePattern = "\d+\s*of\s*\d+"
  15.    Set   reObj = New RegExp
  16.          reObj.Pattern = rePattern
  17.          reObj.Global = False
  18.          reObj.IgnoreCase = True
  19.    'msgbox reObj.Test(strText)
  20.    If reObj.Test(strText) Then
  21.       Dim oCollMatches
  22.       Set oCollMatches = reObj.Execute(strText)   
  23.       If oCollMatches.Count > 0 Then
  24.          Dim strMatch, strMatchValue
  25.          For Each strMatch in oCollMatches
  26.             strMatchValue = strMatch.Value
  27.             strCurrPage = Trim(Mid(strMatchValue, 1, InStr(strMatchValue, "of") - 1))
  28.             strTotPage = Trim(Mid(strMatchValue, InStr(strMatchValue, "of") + 2))
  29.          Next
  30.       End If
  31.       GetCurrTotPage = True
  32.    Else
  33.       msgbox "Please pass the valid string into function!"
  34.    End If
  35. End Function
复制代码
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-9-22 07:35 , Processed in 0.080911 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表