Results 1 to 16 of 16

Thread: Get the string in the middle?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    330

    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

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Get the string in the middle?

    Quote Originally Posted by ne0_b0mb3r View Post
    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

  3. #3
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Re: Get the string in the middle?

    Quote Originally Posted by ne0_b0mb3r View Post
    btw dont tell me to use regex
    Why??
    Visual Studio.net 2010
    If this post is useful, rate it


  4. #4
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Get the string in the middle?

    Quote Originally Posted by ne0_b0mb3r View Post
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    330

    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. -.-

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Get the string in the middle?

    Quote Originally Posted by ne0_b0mb3r View Post
    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.

  7. #7
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: Get the string in the middle?

    regex
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    330

    Re: Get the string in the middle?

    Alright thanks guys, you really helped me alot.
    Not.

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    330

    Re: Get the string in the middle?

    Quote Originally Posted by stanav View Post
    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

  11. #11
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Get the string in the middle?

    vb.net Code:
    1. 'Regex
    2. MessageBox.Show(System.Text.RegularExpressions.Regex.Match("<title>Google</title>", "(?<=>).+?(?=<)").Value)
    3. 'Not Regex
    4. MessageBox.Show("<title>Google</title>".Split(New Char() {">"c, "<"c})(2))

  12. #12
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Get the string in the middle?

    Quote Originally Posted by ne0_b0mb3r View Post
    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 -

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    330

    Re: Get the string in the middle?

    vb Code:
    1. Dim startText As String = "vb"
    2.         Dim endText As String = "rock"
    3.         Dim sourceText As String = "vb forums rock"
    4.         Dim startIndex As Integer = sourceText.IndexOf(startText)
    5.         Dim textInBetween As String = "Not found"
    6.         If startIndex >= 0 Then
    7.             sourceText = sourceText.Substring(startIndex)
    8.             If sourceText.IndexOf(endText) > 0 Then
    9.                 textInBetween = sourceText.Substring(0, sourceText.IndexOf(endText))
    10.             End If
    11.         End If
    12.         MessageBox.Show(textInBetween)
    13.     End Sub

    That's just giving me "vb forums" :/

  14. #14
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Get the string in the middle?

    Quote Originally Posted by ne0_b0mb3r View Post
    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...

  15. #15
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Get the string in the middle?

    Quote Originally Posted by ne0_b0mb3r View Post
    vb Code:
    1. Dim startText As String = "vb"
    2.         Dim endText As String = "rock"
    3.         Dim sourceText As String = "vb forums rock"
    4.         Dim startIndex As Integer = sourceText.IndexOf(startText)
    5.         Dim textInBetween As String = "Not found"
    6.         If startIndex >= 0 Then
    7.             sourceText = sourceText.Substring(startIndex)
    8.             If sourceText.IndexOf(endText) > 0 Then
    9.                 textInBetween = sourceText.Substring(0, sourceText.IndexOf(endText))
    10.             End If
    11.         End If
    12.         MessageBox.Show(textInBetween)
    13.     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

  16. #16
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    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
  •  



Click Here to Expand Forum to Full Width