Micro Focus QTP (UFT) Forums

Full Version: Split a String
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,
I want to know about Split function in qtp.

I want to split a text " New York To Goa"
I want 'New York' and 'Goa' in two separate variable.


Friends got it
text="New York TO Goa"
Myarr=split(text,"TO")
msgbox Myarr(0)
msgbox Myarr(1)
I am facing another problem with Split now.

I have captured the text = " Frankfurt TO New York"
the text come from run time datatable.
text= datatable.Value("Select_a_Flight_MercuryOutput_Text_out",dtglobalsheet)
I used following split function
Code:
Myarr=split(text,"TO")
msgbox Myarr(0)
msgbox  Myarr(1)

Here Myarr(0) displayed the value as Frankfurt TO New York ?
and Myarr(1) displayed following error.

The test run cannot continue due to an unrecoverable error.

Subscript out of range: '[number: 1]'

Line (16): "msgbox Myarr(1)".

Please find the below code
---------------------------------------------------------------------------------
Code:
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set "mercury"
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("password").SetSecure "4d136083f7eb3294cfee0f9766e331cd0c781dde"
Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click 34,13
For i=1 to Datatable.GetRowCount
datatable.SetCurrentRow(i)
Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("fromPort").Select DataTable("p_Item", dtGlobalSheet)
Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("toPort").Select DataTable("p_Item1", dtGlobalSheet)
Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").Image("findFlights").Click 57,16
'Browser("Welcome: Mercury Tours").Page("Select a Flight: Mercury").WebElement("Frankfurt to New York").Output CheckPoint("Frankfurt to New York")
Browser("Welcome: Mercury Tours").Page("Select a Flight: Mercury").Output CheckPoint("Select a Flight: Mercury Tours")
text= datatable.Value("Select_a_Flight_MercuryOutput_Text_out",dtglobalsheet)
msgbox text
Dim a,b,text
Myarr=split(text,"TO")
msgbox Myarr(0)
msgbox  Myarr(1)
Browser("Welcome: Mercury Tours").Back
Next
Hi Sudhir,

Can you please provide the strings you get from the 2 outcheckpoints you applied and also make sure that the values are stored in different columns.

The split is working fine for me.

Code:
text= "Frankfurt TO New York"
Myarr=split(text,"TO")
msgbox Myarr(0)
msgbox Myarr(1)
Hi Manish,
One slight chnage in above code is, I have not used below checkpoint.
Code:
'Browser("Welcome: Mercury Tours").Page("Select a Flight: Mercury").WebElement("Frankfurt to New York").Output CheckPoint("Frankfurt to New York")

Below code is alos working for me, if use seprately.

But if it is implemented with above script, I am surprised it give inorrect result.
For details please see below Datatable I used.

Code:
DataTable("p_Item", dtGlobalSheet)-  For Fly From parameters.
DataTable("p_Item1", dtGlobalSheet)- For Fly To parameters.
datatable.Value("Select_a_Flight_MercuryOutput_Text_out",dtglobalsheet)
- Which give me run time output value.

Now if
Fly From = New York
Fly To= Paris.
I put it in text so
text= datatable.Value("Select_a_Flight_MercuryOutput_Text_out",dtglobalsheet) = "New York To Paris"

Its working upto this.
Now I used Split function to seprate " New York To Paris" using following split function.
Myarr=split(text,"TO")
When I used following function
msgbox Myarr(0)
It still displyed the whole text . i.e. "New York To Paris" instead it should only show New York
And msgbox Myarr(1) displayed error.

Can you please let me know why this is happening.

Simpley I want to verify that
Code:
a= x and b=y where
a= DataTable("p_Item", dtGlobalSheet
b=DataTable("p_Item1", dtGlobalSheet)
x=Myarr(0)
y=Myarr(1)
Hi Sudhir,

You typed the string wrong while performing split:
The string you want to split is: "New York To Paris"
But while spliting you used "TO", it should be "To".
Ohh ok,
Manish I will check it on and let you know.

One more problem i faced yesterday on this is
if i used
msgbox Myarr(0) It displayed the text.
But if I put above value in another variable like
a= Myarr(0) why this shows error ?

Thanks Manish,
I corrected it, and its working now. And my second query also get resolved as I am able to get both the value in
a= Myarr(0)
b= Myarr(1).

Thanks a lot.
Code:
Str="New York TO Goa"
'Split above string with 'TO' as delimiter
SplStr=Split(Str,"TO",-1,1)
Now SplStr is an Array with
SplStr[0]="New York" and
SplStr[1]="Goa"
Hi Prasad,

Wrap-up the code and then post.

Seems you are new member to this forum, plz go thru the Posting guidelines once.

yes srikanth.. recently i joined in the forum.
for next postings will follow the posing guidelines..



Thanks,
UFTEnthusiast