Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast string replacement

I have the following sample string:
(My actual strings are much longer than this)

s = "You must eat lots of fruits and vegetables every day
in the daytime hours."

I'm looking for a fast way to replace an entire set of words
in the above string as follows:

"must" - "should"
"fruits" - "eggs"
"vegetables" - "bacon"
"day" - "morning"

The final output should be:

"You should eat lots of eggs and bacon every morning
in the daytime hours."

The string replacements must occur on word boundaries
only. So, if "daytime" occurs in the string, then I do not
want to change that to "morningtime".

The faster the solution, the better. Would a regular
expression approach be faster than using a "for-each"
construct with a bunch of boolean logic and Instr() calls?





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast string replacement

Given you want word boundaries only, your list of words should include
the character following the word...

Function ReplaceWords$(sText$, sWords$)
Dim sz$, vTmp, v1, n&

v1 = Split(sWords, ","): sz = sText

For n = LBound(v1) To UBound(v1)
vTmp = Split(v1(n), ":")
sz = Replace(sz, vTmp(0), vTmp(1))
Next 'n
ReplaceWords = sz
End Function

Sub Test_ReplaceWords()
Const sWords$ = "must :should ,fruits :eggs ,vegetables :bacon ,day
:morning "
Const sText$ = "You must eat lots of fruits and vegetables every day
in the daytime hours."
Debug.Print ReplaceWords(sText, sWords)
End Sub

--
Garry

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast string replacement

Reposting test routine because it didn't post right 1st time...


Sub Test_ReplaceWords()
Const sWords$ = "must :should ,fruits :eggs ," _
& "vegetables :bacon ,day :morning "
Const sText$ = "You must eat lots of fruits and vegetables every day
in the daytime hours."
Debug.Print ReplaceWords(sText, sWords)
End Sub

--
Garry

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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Fast way to truncate string Robert Crandal[_3_] Excel Programming 30 April 1st 15 11:43 PM
DDE Replacement? Mark Excel Programming 1 September 10th 06 05:30 AM
Replacement T De Villiers[_49_] Excel Programming 3 July 21st 06 08:18 PM
Replacement mowen Excel Discussion (Misc queries) 1 September 7th 05 09:01 PM
Any fast method to parse a string into row & col information Nick Excel Programming 3 August 19th 04 06:47 PM


All times are GMT +1. The time now is 04:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"