View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

Suppose I have the following string.

Dim s as String
s = "Always eat lots of fruits and vegetables every day."

I would like to be able to truncate this string as quickly
as possible, beginning from a word of my choosing, all
the way to the end of the string.

So, if I choose the word "and" as my target word, the
above string should be transformed into:

"Always eat lots of fruits "

Essentially, all text after the first occurence of "and"
was removed from the string.

I'm looking for solution that runs quickly. Thanks!

~Rob


Try...

s = Mid(s, 1, InStr(s, "and") - 1)

OR

s = TruncateString(s, "and")

Function TruncateString(sText, sFind)
TruncateString = Mid(sText, 1, InStr(sText, sFind) - 1)
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion