|
-
Oct 27th, 2006, 11:42 AM
#1
Thread Starter
New Member
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
-
Oct 27th, 2006, 12:20 PM
#2
Re: Finding string in an Array
What's the code you have so far?
-
Oct 27th, 2006, 12:41 PM
#3
Thread Starter
New Member
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:
'start from bottom of loop, I have to do this because of some extra computation that goes on side
For i = (inputArray.Length - 1) To 0 Step i - 1
'take first string as input
Dim toBeMatched As String = inputArray(i)
Response.Write("<BR> to be matched=" & toBeMatched)
Dim k As Integer = 0
For k = 0 To (inputArray.Length - 1) Step k + 1
Dim tempString As String = inputArray(k)
Dim tokenNum As Integer = 0
For tokenNum = 0 To toBeMatched.Length Step tokenNum + 1
Dim token As String = toBeMatched.Chars(tokenNum)
Response.Write("<BR> token=" & token)
'tokenize the string
Dim tt As Boolean = tempString.Contains(token)
If (tt) Then
' 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..
Response.Write("<BR> tt=" & tt & "..tempString=" & tempString)
End If
Next
Next
Next
-
Oct 27th, 2006, 02:10 PM
#4
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:
Public Function FindString(ByVal strToFind As String, _
ByRef strArray As String()) As ArrayList
Dim lstResult As New ArrayList
'Remove the "," and "_" from the search string
Dim myString As String = strToFind.Replace(","c, "")
myString = myString.Replace("_"c, "")
Dim chrArray As Char() = myString.ToCharArray
For Each str As String In strArray
If str.IndexOf(chrArray(0)) >= 0 _
AndAlso str.IndexOf(chrArray(chrArray.Length - 1)) >= 0 Then
lstResult.Add(str)
End If
Next
Return lstResult
End Function
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim result As ArrayList = FindString(TextBox1.Text, tester)
If result.Count > 0 Then
Dim display As String
For i As Integer = 0 To result.Count - 1
display &= DirectCast(result(i), String) & vbCrLf
Next
MsgBox(display)
Else
MsgBox("Nothing")
End If
End Sub
-
Oct 27th, 2006, 04:50 PM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|