VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,477 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,747 people online right now. Registration is fast and FREE... Join Now!




Searching A String For Specific Values

 

Searching A String For Specific Values, Harder than it seems ;)

matto

26 Jun, 2009 - 11:21 AM
Post #1

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

Hi,

I have a program which grabs the source code of a webpage and puts it into a string. I would now like to search the string for two values.

"video_id": "value 1 here"

"t": "value 2 here"

The problem, is that I don't know the position of the values, and I the values change depending on each webpage. All I know, is the text before the value, e.g. "video_id" and "t:"

Here is my code:

CODE
        Dim videoid As String = TextBox1.Text.Trim

        Dim wc As New System.Net.WebClient()
        Dim result As String = wc.DownloadString(videoid)


Any help appreciated, thanks

User is offlineProfile CardPM
+Quote Post


jacobjordan

RE: Searching A String For Specific Values

26 Jun, 2009 - 11:40 AM
Post #2

class Me : Perfection
Group Icon

Joined: 11 Jun, 2008
Posts: 1,493



Thanked: 65 times
Dream Kudos: 1725
My Contributions
Well, once you have downloaded the web page as a string, you could use the IndexOf function to find the index of video_id and t, and then use some Substring and IndexOf functions to parse the value out of the quotes. Or, you could use Regular Expressions, but those might be a bit too complicated for the situation, especially if you are not familiar with them. Try to use the IndexOf and Substring approach on your own, and if you need any help with the coding, i (or someone else) will assist.
User is offlineProfile CardPM
+Quote Post

matto

RE: Searching A String For Specific Values

26 Jun, 2009 - 12:30 PM
Post #3

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

OK thanks a lot. I will look into what you suggested. BTW, thanks for not giving me some code, I'd rather learn about the functions myself instead of copying and pasting code I don't understand smile.gif
User is offlineProfile CardPM
+Quote Post

matto

RE: Searching A String For Specific Values

27 Jun, 2009 - 02:35 AM
Post #4

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

I had a go at it, but I'm finding it hard to understand how to use IndexOf and substring together. Here's what I have so far:

CODE
Dim videoid As String = TextBox1.Text.Trim
        Dim wc As New System.Net.WebClient()
        Dim videosource As String = wc.DownloadString(videoid)

        Dim t As String = videosource.IndexOf("&t=")
        Dim t2 As String = t.Substring(3, 46)

User is offlineProfile CardPM
+Quote Post

Pwn

RE: Searching A String For Specific Values

27 Jun, 2009 - 08:13 AM
Post #5

D.I.C Regular
***

Joined: 25 Nov, 2007
Posts: 360



Thanked: 13 times
My Contributions
indexof returns an integer. you use this integer in your substring to indicate your starting location. You will also need to figure out the length of the string you're looking for, which will be the 2nd parameter to your indexof substring function. Also, unless you know for a fact that your value will be the same length at all times, hard coding values such as length is not recommended. Figuring out how to always have the right number can be tricky.

Edit: wrong function stated

This post has been edited by Pwn: 27 Jun, 2009 - 08:20 AM
User is offlineProfile CardPM
+Quote Post

LoveIsNull

RE: Searching A String For Specific Values

27 Jun, 2009 - 08:17 AM
Post #6

Disbanding my Ignorance
****

Joined: 10 Mar, 2009
Posts: 536



Thanked: 45 times
My Contributions
The IndexOf function returns an integer, which is the location of the first instance of the "value" parameter you specified.
You can then pass the integer value returned by the IndexOf function into the Substring function along with the length of characters you want to retrieve.
Let's say you're looking for "&t=", and that you know you will always want the next 6 characters afterwards.

CODE
        Dim iStart As Integer = theString.IndexOf("&t=")
        Dim strResult As String = theString.Substring(iStart, 10)

strResult will now contain whatever six characters where after the first occurrence of "&t="

Sorry I can't explain much more at the moment, but see if that helps you for now...
User is offlineProfile CardPM
+Quote Post

matto

RE: Searching A String For Specific Values

1 Jul, 2009 - 11:25 AM
Post #7

New D.I.C Head
*

Joined: 18 Apr, 2009
Posts: 40

Thanks for your code. However, when I used it, strResult still contained "&t=". How can I get only a certain number of characters after "&t="?
User is offlineProfile CardPM
+Quote Post

CamoDeveloper

RE: Searching A String For Specific Values

1 Jul, 2009 - 11:31 AM
Post #8

D.I.C Head
Group Icon

Joined: 12 Jun, 2009
Posts: 204



Thanked: 12 times
Dream Kudos: 200
My Contributions
Try this:
CODE

Dim iStart As Integer = theString.IndexOf("&t=")
Dim strResult As String = theString.Substring(iStart + 3, 10)


~Camo

This post has been edited by CamoDeveloper: 1 Jul, 2009 - 11:32 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:32AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month