Link to home
Start Free TrialLog in
Avatar of tclgb
tclgb

asked on

Compare with InStr Check if Array Item is in a String

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"
                   etc....


          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))
        
          Next

'' 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
          Next

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
ASKER CERTIFIED SOLUTION
Avatar of rafrancisco
rafrancisco

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tclgb
tclgb

ASKER

Cheers for quick response.

What a schoolboy error.