Re: Replace Space

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Thanks for the quick reply.
I will take it all into serious consideration.
I thought it would be fun to mess with RegEx.
So far, I have some working code to do simple searches.
I am also trying the Like Search provided in a previous post.
Not much hair left here either ... so I wear a hat.
If you ever see a guy with blurry eyes and a hat ... that's me.

"Larry Serflaten" wrote:


"Bee" <Bee@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote
I am looking for a speedier way to do the following in VB6 rather than coding
up a slow character by character search and replace.

In a possibly large string (user input) I need to find and replace each
space with
space-characters-space.

However, within the string there are areas that may be defined with spaces
that must not be touched. They always have the format { } e.g.
{sometext space someothertext}
or
{sometext} ' no space

Is there an fast way to do this? Regex ?
Not too familiar with Regex.

As others have said, you would probably do better with your own code.
The trick to faster string manipulations is to avoid concatenation as you
work through your text. In other words, allocate enough memory for your
output buffer in one shot, and then transfer over the parts you need.

The example code below replaces the space character with &nbsp without
touching spaces within the curly braces. See if that helps you get started....

LFS


Option Explicit

Private Sub Form_Load()
Debug.Print Custom("Now is the time for {all good men} to come to the aid of their country.")
End Sub

Function Custom(Original As String) As String
Dim work As String, itm As String
Dim src As Long, dst As Long, active As Boolean
Const rpl = "&nbsp "
Const tagStart = "{", tagEnd = "}"

work = String$(Len(Original) * Len(rpl), 0) ' pick a suitable amount....
dst = 1
active = True

For src = 1 To Len(Original)

itm = Mid$(Original, src, 1)
Mid(work, dst, 1) = itm
dst = dst + 1

Select Case itm
Case " "
If active Then
Mid(work, dst) = rpl
dst = dst + Len(rpl)
End If
Case tagStart
active = False
Case tagEnd
active = True
End Select

Next
Custom = Left$(work, dst - 1)
End Function





.



Relevant Pages

  • Re: Replace Space
    ... up a slow character by character search and replace. ... within the string there are areas that may be defined with spaces ... Dim work As String, itm As String ... Dim src As Long, dst As Long, active As Boolean ...
    (microsoft.public.vb.general.discussion)
  • Re: Replace Space
    ... up a slow character by character search and replace. ... within the string there are areas that may be defined with spaces ... Dim work As String, itm As String ... Dim src As Long, dst As Long, active As Boolean ...
    (microsoft.public.vb.general.discussion)
  • Re: string concatenation
    ... > from src to dst to form a string with strlen< size. ... _documented_ to act differently than the OpenBSD versions do. ... Need an efficient and powerful string library for C? ...
    (comp.lang.c)
  • Re: how to determine if files are on same or different file systems
    ... >> appropriate error handling. ... > That's why I'm looking for a way to tell if a particular partition is ... shutil.copy2(src, dst) ... Or use the length of the string and count backwards? ...
    (comp.lang.python)
  • Re: string concatenation
    ... > from src to dst to form a string with strlen< size. ... _documented_ to act differently than the OpenBSD versions do. ... Need an efficient and powerful string library for C? ...
    (comp.unix.programmer)