VB InStr

6 posts, 2 answers
  1. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 23 Jul 2012 Link to this post

    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.



  2. Answer
    Anthony
    Admin
    Anthony avatar
    19 posts

    Posted 23 Jul 2012 Link to this post

    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
  3. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 24 Jul 2012 Link to this post

    Well done and thanks once again, Anthony. Spot-on answers!  :)
  4. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 24 Jul 2012 Link to this post

    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.)
  5. Answer
    Anthony
    Admin
    Anthony avatar
    19 posts

    Posted 24 Jul 2012 Link to this post

    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
  6. Nigel
    Nigel avatar
    53 posts
    Member since:
    Jan 2012

    Posted 25 Jul 2012 Link to this post

    Oh, of course!  Thanks for puting me straight on that, Anthony.  Efficient as always.
Back to Top