QTP截取字符串方法

2012-03-21  胡志超 


不知道大家是否用过QTP的output vaue的方法,比如说页面出现一大堆文字然后中间有个订单号,我们可以用output value的方法把订单号输出保存到参数或变量里面,这在我之前的文章有提到过。不过有的时候我们不需要去输出值,也可以用下面的办法获取值进行计算或其他操作,用法类似于output value,只要输入字符串并给定前置字符串和后置字符串,就可以得到中间的字符串。前置字符串和后置字符串可以为"",前置为空则表示获取字符串从头开始到后置字符串之间的字符串,后置为空则获取前置字符串之后到字符串结尾。具体请看下面的代码吧。

[vb] view plaincopy
'得到两个字符串中间的字符串。     
'例如:     
'text="人民币300元",tBefore="人民币",tAfter="元"     
'使用between(text,tBefore,tAfter)得到300.     
Public Function between(words,wordBefore,wordAfter)     
    t1=InStr(words,wordBefore)     
    If t1=0 Then    
        between=words     
    Else    
        L1=Len(wordBefore)     
        wordsRight=Right(words,Len(words)-t1-L1+1)     
        t2=InStr(wordsRight,wordAfter)     
        If t2=0 Then    
            wordAfter=""    
        End If    
        If wordAfter="" Then    
            t2=Len(wordsRight)+1     
        Else    
            t2=InStr(wordsRight,wordAfter)     
        End If    
        needWord=Left(wordsRight,t2-1)     
    '   needWord=Mid(words,t1+L1,t2-t1-L1)     
        between=needWord     
    End If    
End Function    
418°/4185 人阅读/0 条评论 发表评论

登录 后发表评论