不知道大家是否用過QTP的output vaue的方法,比如說頁面出現一大堆文字然后中間有個訂單號,我們可以用output value的方法把訂單號輸出保存到參數或變量里面,這在我之前的文章有提到過。不過有的時候我們不需要去輸出值,也可以用下面的辦法獲取值進行計算或其他操作,用法類似于output value,只要輸入字符串并給定前置字符串和后置字符串,就可以得到中間的字符串。前置字符串和后置字符串可以為"",前置為空則表示獲取字符串從頭開始到后置字符串之間的字符串,后置為空則獲取前置字符串之后到字符串結尾。具體請看下面的代碼吧。
[vb] view plaincopyprint?
'得到兩個字符串中間的字符串。
'例如:
'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
原文轉自:http://www.anti-gravitydesign.com