Results 1 to 12 of 12

Thread: Split string question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    137

    Split string question

    I have a multiline rich text box of 3 lines.

    i need to find the line with "Player: Your Hand " and get what comes after it, up untill the end of the line. it could be on any 1 of 3 lines.

    what code can i use to extract what comes between that text and end of the line?

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Split string question

    Here is a quick sample that you can modify to suit your needs.

    Code:
    Private Sub Command1_Click()
        Dim s       As String
        Dim data()  As String
        Dim NewData As String
        Dim a       As Long
        Dim lIndex  As Long
        
        s = "1" & vbNewLine & "2 Player: Your Hand " & vbNewLine & "3" & vbNewLine & "4"
        data = Split(s, vbNewLine)
        For a = LBound(data) To UBound(data)
            If InStr(data(a), "Player: Your Hand") > 0 Then
                lIndex = a + 1
                Exit For
            End If
        Next
        For a = lIndex To UBound(data)
            NewData = NewData & data(a) & vbNewLine
        Next
        NewData = Mid$(NewData, 1, Len(NewData) - 2) 'ignore last vbNewLine
        MsgBox NewData
    End Sub
    This will get all the lines after the line where "Player: Your Hand" is existing until it reaches the end of the line. If you want between then just try.
    Code:
    For a = lIndex To UBound(data) - 1
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    137

    Re: Split string question

    that returns 2 and 4...

    Here is example of what i need:

    PokerLast.text:
    Dealer: Dealing cards
    Player: Your cards 5c 9h
    Dealer: Dealing Flop

    It should find the "Player: Your cards ", and return anything between that and end of the line.

    Text2.Text = GetPlayerHand
    Text2.text will be "5c 9h"

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    137

    Re: Split string question

    i tried this:

    Mid$(PokerLast.Text, "Player: Your cards ", 4)

    but each time error says "Type mismatch"

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Split string question

    Code:
    Private Sub Command1_Click()
        Dim s As String
        Dim a As Long
        Dim b As Long
        Const x As String = "Player: Your cards"
        
        s = "PokerLast.Text:" & vbNewLine & _
        "Dealer: Dealing cards" & vbNewLine & _
        "Player: Your cards 5c 9h" & vbNewLine & _
        "Dealer: Dealing Flop"
        
        a = InStr(s, x) + Len(x)
        b = InStr(a + 1, s, vbNewLine)
        MsgBox Mid$(s, a, b - a)
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    137

    Re: Split string question

    Thanks for helping btw. i wont forget you when this is finished

    but... im trying to make this into a function, i see it works when i paste it into a new project and create a command button.. but sthis is where i get stuck:

    Code:
    Private Function GetPlayerHand(Playername As String) As String
        Dim s As String
        Dim a As Long
        Dim b As Long
        Const x As String = playername & ": Your cards"
        
        
        a = InStr(s, x) + Len(x)
        b = InStr(a + 1, PokerLast.Text, vbNewLine)
        GetPlayerHand = Mid$(s, a, b - a)
        End Function
    so i can do this:

    text1.text = GetPlayerHand("JoeYMe")

    but errors?

    i cant understand why

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Split string question

    Try this. You cannot set a dynamic value to a constant that's why.
    Code:
    Private Function GetPlayerHand(Playername As String) As String
        Dim s As String
        Dim a As Long
        Dim b As Long
        Dim x As String
        x = Playername & ": Your cards"   
        
        a = InStr(s, x) + Len(x)
        b = InStr(a + 1, PokerLast.Text, vbNewLine)
        GetPlayerHand = Mid$(s, a, b - a)
    End Function
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    137

    Re: Split string question

    ahh, sorry to go on..

    Code:
    GetPlayerHand = Mid(s, a, b - a)
    That line comes back with error, "Invalid procedure call or argument"

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2009
    Posts
    137

    Re: Split string question

    changed "s" to PokerLast.text and still same error

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Split string question

    Try it like this.
    Code:
    Option Explicit
    
    Private Function GetPlayerHand(ByVal Playername As String, ByVal strTextToSearch) As String
        Dim a           As Long
        Dim b           As Long
        Dim strToSearch As String
        
        strToSearch = Playername & ": Your cards"
        
        a = InStr(strTextToSearch, strToSearch) + Len(strToSearch)
        b = InStr(a + 1, strTextToSearch, vbNewLine)
        GetPlayerHand = Mid$(strTextToSearch, a, b - a)
    End Function
    
    Private Sub Command1_Click()
        Dim s As String
        s = "PokerLast.Text:" & vbNewLine & _
            "Dealer: Dealing cards" & vbNewLine & _
            "dee-u: Your cards 5c 9h" & vbNewLine & _
            "Dealer: Dealing Flop"
        MsgBox GetPlayerHand("dee-u", s)
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: Split string question

    here's another method:
    Code:
    Dim iStart As Integer
     Dim MyText As String
     Dim EndOfLine As Integer
     rtb.SelStart = 0
     
     iStart = rtb.Find("Player: Your Hand", , , rtfWholeWord) + Len("Player: Your Hand")
     rtb.SelStart = iStart
     EndOfLine = rtb.Find(vbCrLf, , , rtfWholeWord)
     rtb.SelStart = iStart
     rtb.SelLength = EndOfLine - iStart
     MyText = rtb.SelText
    MsgBox MyText
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Split string question

    Here is another....

    Code:
    Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, lParam As Any) As Long
        
    Private Const EM_GETLINECOUNT = &HBA
    Private Const EM_GETSEL = &HB0
    Private Const EM_LINEINDEX = &HBB
    Private Const EM_LINELENGTH = &HC1
    Private Const EM_LINEFROMCHAR = &HC9
    Private Const EM_GETLINE = &HC4
    
    Dim RTBLineCount As Long, Charindex As Long
    Dim FirstChar As Long, RowLength As Long, CursorPos As Long, nLine As Long
    Dim Buffer() As Byte, LineText As String
    Dim SearchString As String
    
    Private Sub Form_Load()
        '-- Loading the RTB as per your requirement
        RichTextBox1.Text = "Dealer: Dealing cards" & vbNewLine & _
        "Player: Your cards 5c 9h" & vbNewLine & _
        "Dealer: Dealing Flop"
    End Sub
    
    Private Sub Command1_Click()
        '-- Your Search String
        SearchString = "Player: Your cards"
        '-- Get the string after the search string
        MsgBox GetText(RichTextBox1, SearchString)
    End Sub
    
    Function GetText(rich As RichTextBox, Strg As String)
        '-- Get the Linecount of RTB
        RTBLineCount = SendMessage(rich.hwnd, EM_GETLINECOUNT, 0, 0)
    
        '-- Loop tru the RTB
        For i = 0 To RTBLineCount - 1
            Charindex = SendMessage(rich.hwnd, EM_LINEINDEX, ByVal i, ByVal CLng(0))
        
            '-- Move to the respective line
            rich.SelStart = Charindex
        
            CursorPos = SendMessage(rich.hwnd, EM_GETSEL, 0, ByVal 0&) \ 65536
            nLine = SendMessage(rich.hwnd, EM_LINEFROMCHAR, CursorPos, ByVal 0&)
            FirstChar = SendMessage(rich.hwnd, EM_LINEINDEX, nLine, ByVal 0&)
            RowLength = SendMessage(rich.hwnd, EM_LINELENGTH, FirstChar, ByVal 0&)
            ReDim Buffer(RowLength + 1)
            Buffer(0) = RowLength + 1
    
            SendMessage rich.hwnd, EM_GETLINE, nLine, Buffer(0)
            LineText = Left$(StrConv(Buffer, vbUnicode), RowLength)
            
            If InStr(LineText, Strg) Then
                '-- Function assumes that the text will always
                '-- start with "Player: Your cards" and hence
                '-- I am using Right()
                GetText = Right(LineText, Len(LineText) - Len(Strg))
            End If
        Next i
    End Function
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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