sample string: "123456 - Popo"
i want to know on how to get my final result like this:
variable1 = "123456"
variable2 = "Popo"
thanks!
Printable View
sample string: "123456 - Popo"
i want to know on how to get my final result like this:
variable1 = "123456"
variable2 = "Popo"
thanks!
VB Code:
'You can use the split function: Dim strTest As String Dim strSplits() As String strTest = "123456 - Popo" strSplits = Split(strTest) Debug.Print strSplits(0) Debug.Print strSplits(2)
any other way?
VB Code:
'Another way: Dim strTest As String Dim intPos As Integer strTest = "123456 - Popo" intPos = InStr(1, strTest, " ") 'Debug.Print intPos Debug.Print Left$(strTest, intPos - 1) intPos = InStrRev(strTest, " ") 'Debug.Print intPos Debug.Print Right$(strTest, Len(strTest) - intPos)
VB Code:
strArray = "123456-Popo" strArray = Split(strArray, "-") Variable1 = strArray(0) Variable2 = strArray(1)
OR
VB Code:
strArray = "123456-Popo" strArray = Split("-" & strArray, "-") Variable1 = strArray(1) Variable2 = strArray(2)
OR
VB Code:
strArray = "123456-Popo" strArray = Split("-" & strArray, "-") For e = 1 to UBound(strArray) Variable(e) = strArray(e) Next e
There are multiple ways of parsing so here one:
VB Code:
Private Sub Command1_Click() Dim sText As String Dim res1$, res2$ sText = "123456 - Popo" res1 = Trim(Left(sText, InStr(1, sText, "-") - 1)) res2 = Trim(Mid(sText, InStr(1, sText, "-") + 1)) Debug.Print res1 Debug.Print res2 End Sub
thanks a lot for the help guys!
i got an error in this with this example string "123456 - Popo-Cost"Quote:
Originally Posted by RhinoBull
result1 = "123456 - Popo"
result2 = "Cost"
the result must be
result1 = "123456"
result2 = "Popo-Cost"
cheers
If you want consistent output you have to have consistent input. Or, the old saying - garbage in, garbage out.