Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split a String
#1
Solved: 10 Years, 8 Months ago
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)
Reply
#2
Solved: 10 Years, 8 Months ago
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
Reply
#3
Solved: 10 Years, 8 Months ago
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)
Reply
#4
Solved: 10 Years, 8 Months ago
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)
Reply
#5
Solved: 10 Years, 8 Months ago
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".
Reply
#6
Solved: 10 Years, 8 Months ago
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 ?

Reply
#7
Solved: 10 Years, 8 Months ago
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.
Reply
#8
Solved: 10 Years, 8 Months ago
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"
Reply
#9
Solved: 10 Years, 8 Months ago
Hi Prasad,

Wrap-up the code and then post.

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

Reply
#10
Solved: 10 Years, 8 Months ago
yes srikanth.. recently i joined in the forum.
for next postings will follow the posing guidelines..



Thanks,
UFTEnthusiast
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split function in string nidhishnair 13 63,193 07-07-2020, 03:47 PM
Last Post: helmzshelmz
Question Split Function Chaithra N 0 997 12-18-2019, 11:33 AM
Last Post: Chaithra N
  Split a string with multiple delimiters in VBScript nandha 2 7,882 02-10-2018, 06:44 PM
Last Post: nandha
  Replace a string in a word document with another string rekha.naik 8 14,918 11-14-2013, 12:58 PM
Last Post: pranikgarg
  Split Function diya 2 5,518 11-28-2012, 08:49 PM
Last Post: diya

Forum Jump:


Users browsing this thread: 1 Guest(s)