|
-
Aug 10th, 2009, 06:35 AM
#1
Thread Starter
Hyperactive Member
Get the string in the middle?
I've searched before and could find this function, but now I can't. It had only few lines.
Anyways, if anyone knows how then I'd really appreciate it.
Let's say I have this string: <title>Google</title>
I wanna get google only,
btw dont tell me to use regex
-
Aug 10th, 2009, 06:42 AM
#2
Re: Get the string in the middle?
 Originally Posted by ne0_b0mb3r
btw dont tell me to use regex
Why not, regex is a good solution for finding that? Just because you don't know how to use them, doesn't mean they aren't a viable solution. Your other option would be to do an indexof to find the start element, and then another indexof with the offset of the first indexof to find the end of the element. Then take all the text between those two numbers using SubString.
If you have it loaded into an HTMLDocument, I believe it has a Title property
-
Aug 10th, 2009, 07:02 AM
#3
Fanatic Member
Re: Get the string in the middle?
 Originally Posted by ne0_b0mb3r
btw dont tell me to use regex
Why??
Visual Studio.net 2010
If this post is useful, rate it

-
Aug 10th, 2009, 07:20 AM
#4
Re: Get the string in the middle?
 Originally Posted by ne0_b0mb3r
I've searched before and could find this function, but now I can't. It had only few lines.
Anyways, if anyone knows how then I'd really appreciate it.
Let's say I have this string: <title>Google</title>
I wanna get google only,
btw dont tell me to use regex
It all depend on where is this string from. As Negative0 mention, if its from an HTMLDocument you might have a Title property. If its in a XML file then you'll use XMLDocument methods to get the value but if its just a string then honnestly the Regex is still the simplest method.
Try and give us a little bit more details.
Alex
.NET developer
"No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)
Things to consider before posting.
Don't forget to rate the posts if they helped and mark thread as resolved when they are.
.Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
My fresh new blog : writingthecode, even if I don't post much.
System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0 
-
Aug 10th, 2009, 08:25 AM
#5
Thread Starter
Hyperactive Member
Re: Get the string in the middle?
Omg I don't need to get string from html tags, google title tags were just an example. -.-
-
Aug 10th, 2009, 08:36 AM
#6
Re: Get the string in the middle?
 Originally Posted by ne0_b0mb3r
Omg I don't need to get string from html tags, google title tags were just an example. -.-
Did you even bother to read my post? I gave you an alternative that did not require the text to be HTML. If you provide an example that uses HTML, then you are probably going to get HTML specific solutions.
-
Aug 10th, 2009, 08:40 AM
#7
Re: Get the string in the middle?
-
Aug 10th, 2009, 08:43 AM
#8
Thread Starter
Hyperactive Member
Re: Get the string in the middle?
Alright thanks guys, you really helped me alot.
Not.
-
Aug 10th, 2009, 09:06 AM
#9
Re: Get the string in the middle?
As Negative0 already suggested, if you don't wnat to use regex, you can use the string.indexof function. Something like this:
Code:
Dim startText As String = "whatever the start text here"
Dim endText as string = "whatever the end text here"
Dim sourceText as string = "the string you want to search from here"
Dim startIndex as integer = sourceText.IndexOf(startText)
Dim textInBetween As String = "Not found"
If startIndex >= 0 Then
sourceText = sourceText.SubString(startIndex)
If sourceText.IndexOf(endText) > 0 Then
textInBetween = startText.Substring(0, sourceText.IndexOf(endText))
End If
End If
MessageBox.Show(textInBetween)
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Aug 10th, 2009, 09:32 AM
#10
Thread Starter
Hyperactive Member
Re: Get the string in the middle?
 Originally Posted by stanav
As Negative0 already suggested, if you don't wnat to use regex, you can use the string.indexof function. Something like this:
Code:
Dim startText As String = "whatever the start text here"
Dim endText as string = "whatever the end text here"
Dim sourceText as string = "the string you want to search from here"
Dim startIndex as integer = sourceText.IndexOf(startText)
Dim textInBetween As String = "Not found"
If startIndex >= 0 Then
sourceText = sourceText.SubString(startIndex)
If sourceText.IndexOf(endText) > 0 Then
textInBetween = startText.Substring(0, sourceText.IndexOf(endText))
End If
End If
MessageBox.Show(textInBetween)
That's giving me
Index and length must refer to a location within the string. Parameter name: length
-
Aug 10th, 2009, 09:45 AM
#11
Re: Get the string in the middle?
vb.net Code:
'Regex MessageBox.Show(System.Text.RegularExpressions.Regex.Match("<title>Google</title>", "(?<=>).+?(?=<)").Value) 'Not Regex MessageBox.Show("<title>Google</title>".Split(New Char() {">"c, "<"c})(2))
-
Aug 10th, 2009, 09:48 AM
#12
Re: Get the string in the middle?
 Originally Posted by ne0_b0mb3r
That's giving me
Index and length must refer to a location within the string. Parameter name: length

Change this inner If block
Code:
If sourceText.IndexOf(endText) > 0 Then
textInBetween = startText.Substring(0, sourceText.IndexOf(endText))
End If
To this, and it should be fine
Code:
If sourceText.IndexOf(endText) > 0 Then
textInBetween = sourceText.Substring(0, sourceText.IndexOf(endText))
End If
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Aug 10th, 2009, 10:12 AM
#13
Thread Starter
Hyperactive Member
Re: Get the string in the middle?
vb Code:
Dim startText As String = "vb" Dim endText As String = "rock" Dim sourceText As String = "vb forums rock" Dim startIndex As Integer = sourceText.IndexOf(startText) Dim textInBetween As String = "Not found" If startIndex >= 0 Then sourceText = sourceText.Substring(startIndex) If sourceText.IndexOf(endText) > 0 Then textInBetween = sourceText.Substring(0, sourceText.IndexOf(endText)) End If End If MessageBox.Show(textInBetween) End Sub
That's just giving me "vb forums" :/
-
Aug 10th, 2009, 10:18 AM
#14
Frenzied Member
Re: Get the string in the middle?
 Originally Posted by ne0_b0mb3r
Alright thanks guys, you really helped me alot.
Not.
Be thankful that you got the help you did, instead of being disrespectful. Nobody here is obligated to even help you at all...
-
Aug 10th, 2009, 10:23 AM
#15
Re: Get the string in the middle?
 Originally Posted by ne0_b0mb3r
vb Code:
Dim startText As String = "vb"
Dim endText As String = "rock"
Dim sourceText As String = "vb forums rock"
Dim startIndex As Integer = sourceText.IndexOf(startText)
Dim textInBetween As String = "Not found"
If startIndex >= 0 Then
sourceText = sourceText.Substring(startIndex)
If sourceText.IndexOf(endText) > 0 Then
textInBetween = sourceText.Substring(0, sourceText.IndexOf(endText))
End If
End If
MessageBox.Show(textInBetween)
End Sub
That's just giving me "vb forums" :/
Change a few of the lines:
Code:
Dim startIndex As Integer = sourceText.IndexOf(startText) + startText.Length
Dim textInBetween As String = "Not found"
If startIndex >= startText.Length Then
-
Aug 10th, 2009, 11:26 AM
#16
Addicted Member
Re: Get the string in the middle?
If you know what you're looking for and where it's at you can use the Mid function.
Code:
Dim strVar As String = "<title>Google</title>"
Dim strVal As String = Strings.Mid(strVar, 8, 6)
MsgBox(strVal)
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
|