Compare with InStr Check if Array Item is in a String

Posted on 2005-04-14
Last Modified: 2008-01-09
I have an Array of the Alphabet

I have a dataset that I page through and add the first letter of each category to a string

called sActiveLetters Itry to write links out as Anchors but when I run code it is not returning true.

sActiveLetters is full eg.  abcdefghijklmnoprstvw

My Code is Below

          Dim sRetStr As String = ""
          Dim sCurrentLetter As String = ""
          Dim sLastLetter As String = ""
          Dim sActiveFirstLetters As String = ""
          Dim vAlphabet(26) As String
          vAlphabet(1) = "A"
          vAlphabet(2) = "B"
          vAlphabet(3) = "C"
          vAlphabet(4) = "D"

          For Each dr In CatlistDS.Tables("Cats").Rows
              sCurrentLetter = LCase(Left(Convert.ToString(dr.Item("tbo_Category")), 1))
              If sCurrentLetter <> sLastLetter Then
                  sActiveFirstLetters = sActiveFirstLetters & sCurrentLetter
                  sRetStr = sRetStr & "<a name=""" & sCurrentLetter & """></a>"
                  sRetStr = sRetStr & "<h2>" & sCurrentLetter & "</h2><hr>"
              End If
              sRetStr = sRetStr & "<a class=CatsList >" & Convert.ToString(dr.Item("tbo_Category")) & "</a><BR>"
              sLastLetter = LCase(Left(Convert.ToString(dr.Item("tbo_Category")), 1))

'' now, you've got the categories in a variable called "sRetStr" -- let's write out the alphabet letter list for page anchors:
          For i = 1 To 26
              If InStr(sActiveFirstLetters, vAlphabet(i)) > 0 Then
                  ' then it's valid, write a link!
                  Response.Write("<a class=Listing href=""#" & vAlphabet(i) & """>" & vAlphabet(i) & "</a>&nbsp;&nbsp;")
                  '  Else
                  '' didn't find the letter in my string of active letters, so I guess it's not active...
                  '  Response.Write("<span class=inactiveListing>" & vAlphabet(i) & "</a>&nbsp;")
              End If

It is the 3 lines below that are not executing as planned

If InStr(sActiveFirstLetters, vAlphabet(i)) > 0 Then
                  ' then it's valid, write a link!
                  Response.Write("<a class=Listing href=""#" & vAlphabet(i) & """>" & vAlphabet(i) & "</a>&nbsp;&nbsp;")

Any help would be most appreciated, it is driving me nuts
Question by:tclgb
    LVL 28

    Accepted Solution

    I think the problem is with this line:

    sCurrentLetter = LCase(Left(Convert.ToString(dr.Item("tbo_Category")), 1))

    You are converting the letters to lowercase and your alphabet is all in uppercase.

    Hope this helps.

    Author Comment

    Cheers for quick response.

    What a schoolboy error.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now