Finding string within string?

GeekDrop

Well-known member
Joined
Dec 14, 2006
Messages
161
Location
Chicago
Programming Experience
10+
Hello all, Newbie problem.

What is the proper vb.net way of detecting a string within a string? It seems when i use IndexOf, it only catches it sometimes and other times not. I screenshotted a current non-working one. As you can see in the pic, I have 'Next" as the string to look for, and it's also being shown in the Locals, but when I hit F8 next, it does not go into the Handler. I would appreciate some help on this. :)

capturewr0.jpg
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
If you look through the list of function available in the String class you will find the function "Contains" which returns a boolean. I believe that is what you're looking for.

When you're having a problem like this, it's a good idea to look at the values of methods and variables. You can add a watch by highlighting a section of code, right clicking it, and selecting 'Add Watch'. Then when you step through the code, the watch window will show you the value of the highlighted code. In the case above, the value returned is 0 (most lists in .NET are zero based, "Next" is at the beginning so Indexof returns 0).
 

GeekDrop

Well-known member
Joined
Dec 14, 2006
Messages
161
Location
Chicago
Programming Experience
10+
Aha! Thank you very much, and thank you for taking the time to describe it in more depth.

I did manage to get it to work using Instr, but I am wondering if that is considered bad .net practice? Is Instr considered "Classic VB" and should be avoided?

Also, in terms of speed, is using .contains faster/slower than Instr?
 

rfaricy

Well-known member
Joined
Feb 9, 2006
Messages
46
Programming Experience
5-10
Yes it would be considered bad .NET practice to use InStr. Contains is a native VB override while InStr is a legacy function. Contains is faster and uses less memory.
 

GeekDrop

Well-known member
Joined
Dec 14, 2006
Messages
161
Location
Chicago
Programming Experience
10+
OK, thank you. I just tried using Contains, and it still passes over the handler? Here is what I tried:

VB.NET:
If link.InnerHtml.Contains("Next >") Then

I'm not quite sure what to place a Watch on, since I can already see what InnerHtml contains in the locals window? Any Ideas?
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
OK, thank you. I just tried using Contains, and it still passes over the handler?
First, what do you mean by handler? Are you referring to the code within If/End If?
I'm not quite sure what to place a Watch on, since I can already see what InnerHtml contains in the locals window? Any Ideas?
You would add a watch for the piece of code you want to know the value of. In this case it would be link.InnerHtml.Contains("Next >"). Once the code is in the watch window, you can alter it and see the results.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
A handler is usually referring to a regular Sub procedure that is linked to one or more events, therefore "handling" that event.

Did you solve the problem?
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
I would make the code like this:

VB.NET:
Dim linkStr as String = link.InnerHtml

If linkStr.Contains("Next >") Then
  'whatever
Else
  MessageBox.Show(linkStr)
End If

See what shows in the box if the test fails.

Note that Contains works for whole strings as per this Immediate Window fragment:

VB.NET:
?"Next >".Contains("Next >")
true
 
Top Bottom