This is a migrated thread and some comments may be shown as answers.

VB InStr

5 Answers 48 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nigel
Top achievements
Rank 2
Nigel asked on 23 Jul 2012, 12:20 PM
Hi,

I need to use VB to find the start (and end) postitions of a substring within a string.  I've tried using standard VB InStr as follows:
Dim justTheDates = wholeString.Substring(InStr(wholeString,"Created On"),InStr(wholeString, "EditBack"))

but all I get is a Test Studio error stating "'InStr' is not declared".

Unfortunately I can't get the information using just Test Studio script because the screen that holds the data I need is a legacy screen and doesn't follow good practice in terms of the HTML.  Long term it's on my radar to get that resolved, but for the present I can only capture the value of a whole Div into an extracted variable.  That variable contains a lot of text, but I have to grab two dates that are included.  I can't guarantee the format of the dates (depends on browser culture) so I need to extract them from the whole string.

Any ideas, please?

Thanks,
Nigel, Transition Computing.



5 Answers, 1 is accepted

Sort by
0
Accepted
Anthony
Telerik team
answered on 23 Jul 2012, 08:18 PM
Hello Nigel,

Strings.InStr is a part of Microsoft.VisualBasic.dll. Have you added an assembly reference to it?

I'd advise using String.IndexOf and String.Substring which won't require an additional reference:

Dim full As String = "This entry was created on 1/1/2011 by User A."
Dim i As Integer = full.IndexOf("created on") + 11
Dim [date] As String = full.Substring(i, 8)
 
Log.WriteLine([date])

Which outputs the following to the log:

LOG: 1/1/2011


Regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Nigel
Top achievements
Rank 2
answered on 24 Jul 2012, 07:28 AM
Well done and thanks once again, Anthony. Spot-on answers!  :)
0
Nigel
Top achievements
Rank 2
answered on 24 Jul 2012, 03:12 PM
Actually, Anthony,  I have a follow-up question about VB Assembly Reference.

I've added a reference in Project Settings script options as: 
   Microsoft.VisualBasic, Version=10.0.0.0
Sourced from:
   C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Microsoft.VisualBasic.dll
In the Code Behind I've added:
   Imports Microsoft.VisualBasic

At this point I was expecting that I could use a function like InStr(), as follows:
Dim x As String = "abc"
Dim y As String = InStr(x,"b")
Log.WriteLine("y = " + y.ToString())  'debug           

but I still get Test Log error saying InStr is not declared.  Where have I gone wrong?

(I should point out that I am actually using String.IndexOf and String.Substring, as you recommended;  what I am doing now is trying to learn how to set up a connection to the VB .dll as a learning exercise.)
0
Accepted
Anthony
Telerik team
answered on 24 Jul 2012, 03:29 PM
Hello Nigel,

InStr() is part of Strings and returns an integer:

Dim x As String = "abc"
Dim i As Integer = Strings.InStr(x, "b")
 
Log.WriteLine(i.ToString())


Regards,
Anthony
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Nigel
Top achievements
Rank 2
answered on 25 Jul 2012, 06:58 AM
Oh, of course!  Thanks for puting me straight on that, Anthony.  Efficient as always.
Tags
General Discussions
Asked by
Nigel
Top achievements
Rank 2
Answers by
Anthony
Telerik team
Nigel
Top achievements
Rank 2
Share this question
or