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
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"
Re: Split string question
i tried this:
Mid$(PokerLast.Text, "Player: Your cards ", 4)
but each time error says "Type mismatch"
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
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
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
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"
Re: Split string question
changed "s" to PokerLast.text and still same error
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
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
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