Alex Gordon
Alex Gordon

Reputation: 60711

vb.net searching for a search within a string

lot_no = "lot123"

s.indexof("lot123") does not return zero

whereas

s.indexof(lot_no) returns zero

has anyone seen a problem like this?

what does s contain?

    For Each s As String In split1

Upvotes: 0

Views: 192

Answers (2)

Ralf de Kleine
Ralf de Kleine

Reputation: 11734

K, looked add the code in your other thread. When I execute the following code I get a result, what am I doing wrong?

Public lot__no As String = "<Lot no>928374</Lot no>"
Sub DoSomething()
    Dim temp_string As String = "<beginning of record>ETCETCETC" 
   Dim myDelims As String() = New String() {"<beginning of record>"}
    Dim Split() As String = temp_string.Split(myDelims, StringSplitOptions.None)

    For Each s As String In Split
        If InStr(s, lot__no) <> 0 Then
            Debug.WriteLine("found" + s)
        End If
    Next
End Sub

Upvotes: 2

Ralf de Kleine
Ralf de Kleine

Reputation: 11734

Not sure what you're asking but this code returns -1 / -1

Dim lotnr As String = "lot123"
For Each s As String In "123asd"
    Debug.WriteLine(s.IndexOf("lot123"))
    Debug.WriteLine(s.IndexOf(lotnr))
Next

Use IndexOf this way:

Dim lotnr As String = "lot123"
For Each s As String In "123asd"
    Debug.WriteLine("lot123".IndexOf(s))
    Debug.WriteLine(lotnr.IndexOf(s))
Next

This results in: 3 3 4 4 5 5 -1 -1 -1 -1 -1 -1

Upvotes: 1

Related Questions