Search This Blog

Saturday, March 24, 2012

Reterive Number from a string.

We will do this test using two examples of script.

Here is example 1.
You have an string "You have sucessfully saved your order and your order no is 110."
Now your task is to get order number out of this string i.e. 110.further more you can use this order number in you other scripts. To achieve this follwing steps need to be followed.

1. var1 = "You have sucessfully saved your order and your order no is 110."
' In order to reterive the order number you have to first  determine length of  the message box.

2. msgbox len(Var1)
' Gives length of the variable.In order to get  110 if you determine positon of  "is" you can easily determine position of 110. But  take care that "is" is not duplicated.

3. MyPos = instr (1, var1, "is")
'  Instr returns location of  "is" in string. Here 1 is location form where you want to start your search in string. if search start location is not given search will always start from 1st location of search string ,Var1 is string in which you have to search & "is" the string you are searching in var1.

4. msgbox mypos
' Returns location of "is". now using mid function you can get order number .'mid function is used to reterive specified number of  characters. Mid function is defined as mid (string,start,length)
'Here String is var1.MyPos is starting location where you want to reterieve characters.As we want to reterive whole string after "is" we will not define any length so it will capture complete string after "is". if you want to capture specified number of characters you can define number of charecter you want to reterive.

5. MyOrderNumber1= mid(var1,MyPos) 
 'Using this expression you will get order number but it will not be in integer form it will be "is 110."

6. msgbox MyOrderNumber1
'As you know the starting location of "is"  (MyPos) & you know that order number start after 3rd location from starting location of " is"  we add  +3 in MyOrderNumber so that it will give you 100, as order number start from 3rd location from "is"same way if there are more characters you can add them.

7. MyOrderNumber2=mid(var1,MyPos+3)
8. msgbox MyOrderNumber2

'In variable myordernumber you will get order number "110." but  you may required to reterive only 100 & remove this dot .As we have defined in line number11 for Mid function, we have not taken length after position of "is" . Lets say we take length as 3 so myordernumber=mid(var1,MyPos+3,3) will be used & it  will give you myordernumber = 100.

9. MyOrderNumber3=mid(var1,mypos+3,3)

10. msgbox MyOrderNumber3

Example 2 :

1. var1= "You have sucessfully saved your order, your order no is 2564359. You can edit your order details in future."

2. msgbox len (var1)

3. MyPos1 = instr(1,var1,"is")
'Reterive position of "is"
4. msgbox MyPos1

5. Str1=mid (var1,MyPos1+3)
'Reterive starting position of order number as order number is starting 3rd character from starting position of "is".
6. msgbox Str1

7. MyPos2=instr(1,str1,".")
'Reterive position of dot.as we already know starting position of order number if we know ending postion of order number we can extract using mid function which Returns a specified number of characters from a string.

8. msgbox MyPos2

9. Str2=mid (Str1,1,MyPos2-1)           
'  -1 have taken, to remove extra dot.You can use MyPos2 as well but it will show ordernumber with dot.
10. msgbox str2

1 comment:

  1. the easy way to learn qtp and you covered basic knowledge on qtp . QTP Tutorials

    ReplyDelete