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
Re: Get the string in the middle?
Quote:
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
Re: Get the string in the middle?
Quote:
Originally Posted by
ne0_b0mb3r
btw dont tell me to use regex
Why??
Re: Get the string in the middle?
Quote:
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.
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. -.-
Re: Get the string in the middle?
Quote:
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.
Re: Get the string in the middle?
Re: Get the string in the middle?
Alright thanks guys, you really helped me alot.
Not.
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)
Re: Get the string in the middle?
Quote:
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
:p
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))
Re: Get the string in the middle?
Quote:
Originally Posted by
ne0_b0mb3r
That's giving me
Index and length must refer to a location within the string. Parameter name: length
:p
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
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" :/
Re: Get the string in the middle?
Quote:
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...
Re: Get the string in the middle?
Quote:
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
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)