Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing String Question 7

Status
Not open for further replies.

jsmokey

Technical User
Aug 19, 2005
10
US
Hi,
lets say that i have a string like this

StringA = "help me please guys"

and i have another string variable that i want to put the word "guys" in. The only problem is that this is a tab delimited text file so all the spaces that you see in stringa are actually tabs...so my question is how do i start from the end of the string, and just cut off everything before the last tab and save it into stringb? thanks
 
stringb = split(stringa, vbTab)(ubound(Split(stringa, vbTab)))


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Four methods off the top of my head (untested)

Option 1.
Code:
Dim Words()As String

Words = Split(StringA, vbTab)

StringB = Words(Ubound(Words))
Option 2.
Code:
StringB = StringA
Do While Instr(StringB, vbTab)
   StringB = Mid(StringB, Instr(StringB, vbTab) + 1)
Loop
Option 3.
Code:
Dim Ptr As Integer

Ptr = Instr(StringA, vbTab) + 1
Do While Instr(Ptr, StringA, vbTab)
   Ptr = Instr(Ptr, StringA, vbTab) + 1
Loop

StringB = Mid(StringA, Ptr)
Option 4.
Code:
Dim Ptr As Integer

Ptr = Len(StringA)
Do Until Mid(StringA, Ptr, 1) = vbTab
   Ptr = Ptr - 1
Loop

StringB = Mid(StringA, Ptr + 1)

Trevor
 
Tab has an ascii value of 9. It's represented either as chr(9) (chr$(9) is basically equivalent, rather more efficient) or with the constant vbTab. Therefore this code sample will evaluate characters 1 by 1 from the right until it encounters a tab, and then return the characters to the left of that point:
Code:
Private Function doStringA() As String
Dim i As Integer
Dim StringA As String
StringA = "Help" & vbTab & "is" & vbTab & "on" & vbTab & "the" & vbTab & "way"
i = Len(StringA)
Do Until Mid$(StringA, i, 1) = vbTab
    i = i - 1
    If i = 0 Then 'in case there are no tabs, avoid endless loop
        Exit Do
    End If
Loop
doStringA = Left(StringA, i)
End Function

I'm sure this can be done much more efficiently with Regular expressions. Perhaps strongm or someone else will give an example of how to do that.

HTH

Bob
 
I like DrJavaJoe's solution. It's how I would have done it.

Looking at Trevor's post got me to thinking...

This would have worked also.

Code:
StringA = "help" & vbTab & "me" & vbTab & "please" & vbTab & "guys"
StringB = Mid(StringA, InStrRev(StringA, vbTab) + 1)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
It is probably more efficient to not use Regular Expressions with this, but if you wanted to, I believe this pattern would work:

"^(.*)\t.*$"

The part you want should be in the firs submatch returned.

(untested)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Ah! Joe's is definitely the best. Of course you can take the last array value in split. I like your InStrRev solution too, George. Mine is a dumb idea, unless you're using vb4 or something.

Bob
 
>stringb = split(stringa, vbTab)(ubound(Split(stringa, vbTab)))

Somebody evil introduced you to this sort of thing ...
 
MsgBox StrReverse(Mid(StrReverse(StringA), 1, InStr(StrReverse(StringA), " ")))

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
>stringb = split(stringa, vbTab)(ubound(Split(stringa, vbTab)))

Somebody evil introduced you to this sort of thing ...


I had to stare at it for a while to figure out what was going on ... all the while thinking "... OMG, I've forgotten how to program ...".

Not a nice thing to do to an old guy.
 
Or,

MsgBox Mid(stringA, Len(stringA) - InStr(StrReverse(stringA), " "))

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
strongm: aka Mr. Evil ;-)


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
<Somebody evil introduced you to this sort of thing ...
Probably a C++ programmer....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top