Results 1 to 21 of 21

Thread: How do I get the end of a string?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    How do I get the end of a string?

    Ok, I'm using this function:

    VB Code:
    1. Public Function GetStringBetween(ByVal InputText As String, _
    2.     ByVal StartText As String, _
    3.     ByVal EndText As String, _
    4.     Optional ByVal StartPosition = 1) As String
    5.  
    6.     Dim lnTextStart As Long
    7.     Dim lnTextEnd As Long
    8.  
    9.     lnTextStart = InStr(StartPosition, InputText, StartText, vbTextCompare) + Len(StartText)
    10.     lnTextEnd = InStr(lnTextStart, InputText, EndText, vbTextCompare)
    11.     If lnTextStart >= (StartPosition + Len(StartText)) And lnTextEnd > lnTextStart Then
    12.         GetStringBetween = Mid$(InputText, lnTextStart, lnTextEnd - lnTextStart)
    13.     Else
    14.         GetStringBetween = ""
    15.     End If
    16. End Function

    and I need to get the string "</B> J. S. Hodson, London : 1852."

    So I'm using: strName = "GetStringBetween(strHTML), "</b>", "notsurewhattoputhere")"

    I need to get between </B>, and the very end of the string, so that I would get "J. S. Hodson, London : 1852."

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: How do I get the end of a string?

    What comes after the ": 1852." ? Will the year always be the same?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How do I get the end of a string?

    use split(), this wil work is your HTML contains more than 1 line

    VB Code:
    1. strName = split(Split(strHTML), "</b>")(1), vbCrLf)(0)
    2. 'strNmae will contain everything after the <br> to the end of the line
    Chris

  4. #4
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: How do I get the end of a string?

    GetStringBetween = Right(strHTML, len(strHTML) - instrrev(strHTML,"</b>"))
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    Well heres the code...

    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             strName3(iiii) = GetStringBetween(strHTML), "</b>", "notsurewhattoputhere")
    6.                             Next iiii
    7.                             MsgBox strName3(1)

    and heres what the arrays are after it's split:
    /cgi/t/text/text-idx?c=hearth;idno=4088776'>The American cotton spinner and managers' and carders' guide; a practical treatise on cotton spinning; giving ...</A> Baird, Robert H.</B> Henry Carey Baird, Philadelphia : 1863.
    So i'm trying to get the blue text

  6. #6
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How do I get the end of a string?

    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             [B]strName3(iiii) = Split(strHTML, "</b>")(1)[/B]
    6.                             Next iiii
    7.                             MsgBox strName3(1)
    its a simple as that^ the only line i changed was the bold line, the (1) indicates you want everything after the 1st </b>, if you changed it to 0 it would get everything before the </b>, and so on..
    Chris

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             strName3(iiii) = Split(arrName3(iiii), "</b>")(1)
    6.                             Next iiii
    7.                             MsgBox strName3(1)

    This gives me a script out of range error, pointing to "strName3(iiii) = Split(arrName3(iiii), "</b>")(1)"

  8. #8
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How do I get the end of a string?

    Quote Originally Posted by SuffocateMyMind
    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             strName3(iiii) = Split(arrName3(iiii), "</b>")(1)
    6.                             Next iiii
    7.                             MsgBox strName3(1)

    This gives me a script out of range error, pointing to "strName3(iiii) = Split(arrName3(iiii), "</b>")(1)"
    change the b to a cap B so
    VB Code:
    1. strName3(iiii) = Split(arrName3(iiii), "</[B]B[/B]>")(1)
    you might want to also remove the space, as there will be a space at the front of the new variable, so you could use
    VB Code:
    1. strName3(iiii) = Split(arrName3(iiii), "</B> ")(1)
    [/Highlight]
    but dont forget if you do it that way^ and if theres no space after the </B> it will giev error 9 - subscript outta range
    Chris

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             strName3(iiii) = Split(arrName3(iiii), "</B> ")(1)
    6.                             Next iiii
    7.                             MsgBox strName3(1)

    Still getting the same error...

  10. #10
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How do I get the end of a string?

    Quote Originally Posted by SuffocateMyMind
    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             strName3(iiii) = Split(arrName3(iiii), "</B> ")(1)
    6.                             Next iiii
    7.                             MsgBox strName3(1)

    Still getting the same error...
    take away the space after the </B> and see if it works, that error means that it cant find '</B> ' in the string, so if you still get the error, put a msgbox in to show the string, to make sure </B> exists there, and there is data after the </B>
    Chris

  11. #11

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    It still doesn't work, and arrName3 = /cgi/t/text/text-idx?c=hearth;idno=4400488'>A practical treatise on dyeing and calico-printing; including the latest inventions and improvements...</A> Parnell, Edward A.</B> Harper & brothers, New York : 1846.

    So i'm sure that it's there...

  12. #12
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How do I get the end of a string?

    try
    VB Code:
    1. For iiii = 0 To UBound(arrHTML) [B]-1[/B]
    Chris

  13. #13

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    nope...

  14. #14
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How do I get the end of a string?

    if you comment out the Split line and just stick a msgbox there, does it still error?
    Chris

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             'strName3(iiii) = Split(arrName3(iiii), "</B>")(1)
    6.                             MsgBox strName3(1)
    7.                             Next iiii

    it just shows an empty msgbox

  16. #16

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    or if i do


    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             'strName3(iiii) = Split(arrName3(iiii), "</B>")(1)
    6.                             MsgBox arrName3(1)
    7.                             Next iiii

    then i get

    /cgi/t/text/text-idx?c=hearth;idno=4400488'>A practical treatise on dyeing and calico-printing; including the latest inventions and improvements...</A> Parnell, Edward A.</B> Harper & brothers, New York : 1846.

  17. #17
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How do I get the end of a string?

    I think the last item might be blank actually or all of the items are, because it was a newline in the strHTML, it would be great if you could upload the project or just send it to me
    Chris

  18. #18

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    well the project is not even close to done and it's really messy

  19. #19
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: How do I get the end of a string?

    try
    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             strName3(iiii) = Split(arrName3(iiii), "</B>")(1)
    6.                             Next iiii
    Chris

  20. #20

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    still getting an error... I don't understand why I would use split tho... why can't I use GetStringBetween

  21. #21

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    34

    Re: How do I get the end of a string?

    I fixed it, I just added a "~" to the end of arrName3

    VB Code:
    1. arrName3 = Split(strHTML, "<LI><B><A HREF='")
    2.                             Dim iiii As Long
    3.                             ReDim strName3(UBound(arrHTML))
    4.                             For iiii = 0 To UBound(arrHTML)
    5.                             strName3(iiii) = GetStringBetween(arrName3(iiii) & "~", "</b>", "~")
    6.                             Next iiii
    7.                             MsgBox strName3(1)

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