Results 1 to 5 of 5

Thread: Finding string in an Array

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    6

    Finding string in an Array

    I have an string array something like

    Dim tester() As String = {"5,", "4,", "5_4,", "5_7,", "7_6,", "5_6_7,", "5_7_6_4,"}

    Yes, there is a comma at the end of each string.

    I want to find positions in array tester where string "5" exists. What I am not able to solve is if I have to find string "5_7" - the result I need is "5_6_7," and
    "5_7_6_4".

    So if my input is {5_7}, I am looking for all string in that array that have {5 and 7} in some form.

    Is this possible to do? or is there is better way to do this ?

    I have tried for like few hours now and nothing works

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Finding string in an Array

    What's the code you have so far?

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    6

    Re: Finding string in an Array

    Here is the code. What I trying to do is take first string as input, take each char in that as seperate token and then loop thru complete array and store all indexes where match occurs (for all tokens).

    inputArray is one that holds the data

    VB Code:
    1. 'start from bottom of loop, I have to do this because of some extra computation that goes on side
    2.         For i = (inputArray.Length - 1) To 0 Step i - 1
    3.            'take first string as input
    4.            Dim toBeMatched As String = inputArray(i)
    5.             Response.Write("<BR> to be matched=" & toBeMatched)
    6.             Dim k As Integer = 0
    7.             For k = 0 To (inputArray.Length - 1) Step k + 1
    8.                 Dim tempString As String = inputArray(k)
    9.                 Dim tokenNum As Integer = 0
    10.                 For tokenNum = 0 To toBeMatched.Length Step tokenNum + 1
    11.                     Dim token As String = toBeMatched.Chars(tokenNum)
    12.                     Response.Write("<BR> token=" & token)
    13.                     'tokenize the string
    14.                     Dim tt As Boolean = tempString.Contains(token)
    15.                     If (tt) Then
    16.                         ' now here is the problem, I want only those indexes where all tokens exists...I should move to next token and store boolean val in some temp variable and...I am little confused here..
    17.                         Response.Write("<BR> tt=" & tt & "..tempString=" & tempString)
    18.                     End If
    19.                 Next
    20.             Next
    21.  
    22.         Next

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Finding string in an Array

    Try this code (it only tests for the 1st and the last numbers of the search string, i.e. "5_7,". If your search string contains more numbers, i.e. "5_6_9," then you will need to modify it... By no means this is the final solution but rather just something to give you an idea)
    VB Code:
    1. Public Function FindString(ByVal strToFind As String, _
    2.                                ByRef strArray As String()) As ArrayList
    3.         Dim lstResult As New ArrayList
    4.         'Remove the "," and "_" from the search string
    5.         Dim myString As String = strToFind.Replace(","c, "")
    6.         myString = myString.Replace("_"c, "")
    7.         Dim chrArray As Char() = myString.ToCharArray
    8.         For Each str As String In strArray
    9.             If str.IndexOf(chrArray(0)) >= 0 _
    10.             AndAlso str.IndexOf(chrArray(chrArray.Length - 1)) >= 0 Then
    11.                 lstResult.Add(str)
    12.             End If
    13.         Next
    14.         Return lstResult
    15.     End Function
    16.  
    17.     Private Sub Button1_Click(ByVal sender As System.Object, _
    18.                        ByVal e As System.EventArgs) Handles Button1.Click
    19.  
    20.         Dim result As ArrayList = FindString(TextBox1.Text, tester)
    21.         If result.Count > 0 Then
    22.             Dim display As String
    23.             For i As Integer = 0 To result.Count - 1
    24.                 display &= DirectCast(result(i), String) & vbCrLf
    25.             Next
    26.             MsgBox(display)
    27.         Else
    28.             MsgBox("Nothing")
    29.         End If
    30.  
    31.     End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    6

    Re: Finding string in an Array

    thanks for the code. I haven't tried the code yet but will it work for 2 digits input? Like input 10_11?

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width