none
Using the INSTR function RRS feed

  • Question

  • Typically I use the INSTR function the following way:

    If Instr(dum,"1-0")>0 then

    do something

    Endif

    Is there a way, without writing a routine to accomplish this:

    if Instr(dum, "[0-9][-][0-9]")>0 then

    do something

    Endif

    Although my string I am looking for is 1-0 as long as I look for the above string first (1-0) then if its not found it will move down to the next if statement

    If Instr(dum,"1-0")>0 then

    do something

    elseif Instr(dum, "[0-9][-][0-9]")>0 then     purpose is to look for any string made up of the other possibilities ie 1-7 or 2,1 etc)

    do something

    Endif

    Or perhaps there is another string function that gets it done also.

    Thanks

    Les

    Wednesday, November 29, 2017 1:17 PM

Answers

  • I looked at the link you gave me and I dont seem to be able to work with it.  After looking into it deeper I realized it was not VB but c# for the most part.

    The regex class in .Net works the same in VB as it does in C# (and the other .Net languages).

    • Marked as answer by Les2011 Thursday, November 30, 2017 9:56 PM
    Thursday, November 30, 2017 6:42 AM

All replies

  • Although the Instr function should do it job, is it a little bit antique in use. 

    In most program languages the first item is 0 and not 1 likewise in VB. The newer way is inherited from a very old use machine way of programming. Nevertheless, not 1 but 0 is the first in almost all for human program languages.

    Therefore the Substring method is easier to use. 

    https://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx

    Beside that the Instr is a little bit complex (although it is the fastest function to cut strings).


    Success
    Cor


    • Edited by Cor Ligthert Wednesday, November 29, 2017 1:27 PM
    • Proposed as answer by Frank L. Smith Wednesday, November 29, 2017 1:37 PM
    Wednesday, November 29, 2017 1:26 PM
  • Try this too:

    If InStr(dum, "1-0") > 0 Then
       '. . .
    ElseIf dum Like "*[0-9]-[0-9]*" Then
       '. . .
    End If

    See also: Regular Expressions and the Regex class.



    • Edited by Viorel_MVP Wednesday, November 29, 2017 1:34 PM
    • Proposed as answer by Frank L. Smith Wednesday, November 29, 2017 1:37 PM
    Wednesday, November 29, 2017 1:32 PM
  • Well this routine will not work I don't believe since I have to search for the string within a string.  Your routine will work if I make a direct comparison but will not search through a string like the following:

    dum= "This is a test 1-0 to see if it works"

    Your first test finds the 1-0 however will not in the second part of the if statement.  What if I have this it means its no good.

    dum= "This is a test 2-7 to see if it works"  It will not see the 2-7

    Thank you for your input.

    Les

    Wednesday, November 29, 2017 2:15 PM
  • Hi Cor,

    I looked at that link you were kind enough to point me to but I don't see how I can use it to search for the presence of the substrings I am trying to test for.  Only way I can see is I would have to write a routine that moves the len one character at a time and have to test it.  I am trying to stay away from writing code like that and perhaps you had something different in mind, let me know.

    Thx Les

    Wednesday, November 29, 2017 11:50 PM
  • Hi

    Difficult to kniow if I am on the right track here. I am imagining (since you don't say) that you have some sort of list or perhaps a groups of TextBoxes that contains what you are looking for. Anyway, here is some code that you can ignore if I am way off base.

    This one uses a list of search items and a results list of Found or Not Found. The results list can be used to determine whether or nit to execute some other code.

            Dim dum As String = "A test case string containing 10-12 points against a possible 12/12 available points. 10/12 is still a good rating, but 12 of 12 would be better."
    
            ' list of search items
            Dim lst As New List(Of String)
            lst.AddRange({"10-12", "10/12", "10\12", "10 of 12", "12 - 12", "12 of 12"})
    
            Dim results As New List(Of String)
    
            For Each s As String In lst
                Select Case dum.ToLower.Contains(s.ToLower)
                    Case True
                        results.Add(s & " Found")
                    Case Else
                        results.Add(s & " Not Found")
                End Select
            Next
            ' results = Found, Found, Not Found, Not Found, Not Found, Found


    Regards Les, Livingston, Scotland

    Thursday, November 30, 2017 12:24 AM
  • Is there a way, without writing a routine to accomplish this:

    if Instr(dum, "[0-9][-][0-9]")>0 then

    do something

    Do you mean
      If dum includes the substring that consists of any digit from 0 to 9 followed by "-" followed by any digit from 0 to 9 Then do something

    A regular expressions would be the correct way to do that.
    https://docs.microsoft.com/en-us/dotnet/standard/base-types/the-regular-expression-object-model

    It might be possible to work up an expression in code, but it would have to deal with odd cases, such as "1--2" and "-1" and "1-" and "-1-2", so it actually becomes quite complex.

    Thursday, November 30, 2017 12:38 AM
  • Hi Acamar,

    I looked at the link you gave me and I dont seem to be able to work with it.  After looking into it deeper I realized it was not VB but c# for the most part.

    Thx for your thoughts,

    Les

    Thursday, November 30, 2017 6:04 AM
  • I looked at the link you gave me and I dont seem to be able to work with it.  After looking into it deeper I realized it was not VB but c# for the most part.

    The regex class in .Net works the same in VB as it does in C# (and the other .Net languages).

    • Marked as answer by Les2011 Thursday, November 30, 2017 9:56 PM
    Thursday, November 30, 2017 6:42 AM
  • Hi Acamar and thanks for your quick response.  As far as your previous post the answer is yes, I have to be able to find [0-9][-][0-9] so I can say it is an error.  I am not very familiar with regex except in a very elementary way but I will give it a read.  I guess working with instr is not an option because I guess it does not search through a string other then for a specific substring and not like the regex example I have in the top.

    Thx once again for your time,

    Les

    Thursday, November 30, 2017 6:47 AM
  • I guess working with instr is not an option because I guess it does not search through a string other then for a specific substring and not like the regex example I have in the top.

    You can search for a specific string, but you have to construct the string to search for.  You could do that with two loops - an outer loop going from 0 to 9, and an inner one going from 0 to 9, so that you create the strings "0-0", "0-1", "0-2"... "1-0", "1-1", "1-2" and so on.  Then search for each.  But I presume that is the 'routine' that you are trying to avoid.  A regex will do it in a few statements - one to create the regex and one to execute it. You just have to put the time into constructing the regex.

    Thursday, November 30, 2017 7:23 AM
  • Hi Acamar

    Thanks for your suggestion using regex.  I was able to construct I think a workable expression and will test it more thoroughly tonight but initially it looks like it does the trick.  Actually there were 2 strings I needed to search for.  One was 0-1 and the other was 1/2-1/2 but keeping in mind that ll the numbers can be anything from 0-9. Following is my expression:

    Dim Match1 As Object
    Dim EvalVersion2 As String
    Dim temp As String = "This is a test to see if it works 0-4"
    EvalVersion2 = "([0-9][-][0-9])|([0-9][\/][0-9][-][0-9][\/][0-9])"
    Match1 = Regex.Match(temp, EvalVersion2, RegexOptions.None)
    temp = Trim(Match1.ToString)

    Want to thank you and everyone else for your help

    Les

    Thursday, November 30, 2017 1:58 PM