Replace Space 
Author Message
 Replace Space

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.



Thu, 02 Feb 2012 06:28:01 GMT  
 Replace Space


Quote:
> 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.

Regular Expressions allow one to create macro instructions to perform this
kind of parsing. It takes a little practice, but you can find many examples
of RegExps on the web. Likely one that will do exactly what you are looking
for "replace here, but not if bounded there".

However, you had to say "fast". Regular Expressions, once you get a feel for
them, are 'fast' to write and test - thus a worthy tool that should be added
to every developer's skillset - but NO regular expression engine is faster
than a custom char by char routine in code. RegExp engines are on the order
of 3 to 10x slower (depending on how the expression is written, etc.).

Custom routines often look 'slower' because of the amount of code -
especially when compared to the simplicity of a single regular expression
statement. - but most are simple loops with simple conditionals - once the
optimizing compiler gets done - the code runs pretty darn fast.

So if you want 'fast' at runtime - start coding.

If you want 'fast' development start learning Regular Expressions.

-ralph



Thu, 02 Feb 2012 07:03:12 GMT  
 Replace Space

Quote:

> 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.

I'd echo Ralph; I don't have actual figures on regexp as compared to
direct code but this is a pretty straightforward case (dare I say almost
trivial??? :) ).

I'd suggest writing the VB solution and _IF_ (the proverbial "big if")
and when it turns out to be a bottleneck, _then_ worry about optimization.

It's really only a single logical to check if are inside the restricted
space; otherwise make the substitution.  I'd think that actually would
go pretty quickly when compiled.  If VB were slow I'd rewrite it in
fortran or C as a dll as my next step.

--



Thu, 02 Feb 2012 07:21:53 GMT  
 Replace Space


Quote:



> > 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

As a supplement to my response I attempted to write out an expression to
solve this.
The case where one wants to capture a named group between two identical
chars - double quotes (e.g., " this    stuff is      safe"), is easy - but I
find I'm stumbling over defining a group that is encased with two separate
chars "{" and "}".

So if anyone would like to provide a solution it would help to preserve what
little hair I have left. <grin>

-ralph



Thu, 02 Feb 2012 07:47:04 GMT  
 Replace Space


Quote:
> 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



Thu, 02 Feb 2012 09:21:04 GMT  
 Replace Space

Quote:


>> 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.

> I'd echo Ralph; I don't have actual figures on regexp as compared to
> direct code but this is a pretty straightforward case (dare I say almost
> trivial??? :) ).

> I'd suggest writing the VB solution and _IF_ (the proverbial "big if")
> and when it turns out to be a bottleneck, _then_ worry about optimization.

> It's really only a single logical to check if are inside the restricted
> space; otherwise make the substitution.  I'd think that actually would
> go pretty quickly when compiled.  If VB were slow I'd rewrite it in
> Fortran or C as a dll as my next step.

And, of course, you don't actually have to step through character by
character w/ mid$, you can pass that off to InStr to find the opening
"{" character and then the spaces before that position in the string and
then the closing "}" then pick up any remaining spaces.

--



Thu, 02 Feb 2012 09:19:32 GMT  
 Replace Space
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.
Quote:


> > 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



Thu, 02 Feb 2012 09:49:01 GMT  
 Replace Space

Code works.  Thanks!
What is Const rpl = "&nbsp "
What does &nbsp stand for?  non-beaking space?
Litteral insertion?
Minor point.  Gaining knowledge.

Quote:


> > 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



Thu, 02 Feb 2012 10:00:01 GMT  
 Replace Space
Dude, ever heard of Google?

http://en.wikipedia.org/wiki/Non-breaking_space

--
2025
If you do not believe in time travel,
your beliefs are about to be tempered.

http://www.facebook.com/group.php?gid=43606237254

|
| Code works.  Thanks!
| What is Const rpl = "&nbsp "
| What does &nbsp stand for?  non-beaking space?
| Litteral insertion?
| Minor point.  Gaining knowledge.
|
|
| >

| > > 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
| >
| >
| >
| >
| >



Thu, 02 Feb 2012 10:33:39 GMT  
 Replace Space
Come on dude, I was asking why Larry put that in there and for what purpose.
Google cannot read Larry's mind.  Can you?
Quote:

> Dude, ever heard of Google?

> http://en.wikipedia.org/wiki/Non-breaking_space

> --
> 2025
> If you do not believe in time travel,
> your beliefs are about to be tempered.

> http://www.facebook.com/group.php?gid=43606237254


> |
> | Code works.  Thanks!
> | What is Const rpl = "&nbsp "
> | What does &nbsp stand for?  non-beaking space?
> | Litteral insertion?
> | Minor point.  Gaining knowledge.
> |

> |
> | >

> | > > 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
> | >
> | >
> | >
> | >
> | >



Thu, 02 Feb 2012 12:31:01 GMT  
 Replace Space
Hi Ralph,

You could use a RegEx pattern such as "(?<!{((?!})[\s\S])*) "
 Note that's VB.NET syntax, you'd have to ensure that works with the
VBScript regular expression engine.
In VB.NET the usage would be:

Dim output = Regex.Replace(strIn, pattern, " characters ")


Quote:





>> > 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

> As a supplement to my response I attempted to write out an expression to
> solve this.
> The case where one wants to capture a named group between two identical
> chars - double quotes (e.g., " this    stuff is      safe"), is easy - but
> I
> find I'm stumbling over defining a group that is encased with two separate
> chars "{" and "}".

> So if anyone would like to provide a solution it would help to preserve
> what
> little hair I have left. <grin>

> -ralph



Thu, 02 Feb 2012 12:39:10 GMT  
 Replace Space
You asked:  "What does &nbsp stand for?  non-beaking space?"

To which I made my Google comment.

--
2025
If you do not believe in time travel,
your beliefs are about to be tempered.

http://www.facebook.com/group.php?gid=43606237254

| Come on dude, I was asking why Larry put that in there and for what
purpose.
| Google cannot read Larry's mind.  Can you?
|
|
|
| > Dude, ever heard of Google?
| >
| > http://en.wikipedia.org/wiki/Non-breaking_space
| >
| > --
| > 2025
| > If you do not believe in time travel,
| > your beliefs are about to be tempered.
| >
| > http://www.facebook.com/group.php?gid=43606237254


| > |
| > | Code works.  Thanks!
| > | What is Const rpl = "&nbsp "
| > | What does &nbsp stand for?  non-beaking space?
| > | Litteral insertion?
| > | Minor point.  Gaining knowledge.
| > |

| > |
| > | >

| > | > > 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
| > | >
| > | >
| > | >
| > | >
| > | >
| >
| >
| >



Thu, 02 Feb 2012 13:26:35 GMT  
 Replace Space
Here pushing your bull$hit again McDarthy.  GO troll elsewhere where someone
might actually give a damn.

--
2025
If you do not believe in time travel,
your beliefs are about to be tempered.

http://www.facebook.com/group.php?gid=43606237254

| Hi Ralph,
|
| You could use a RegEx pattern such as "(?<!{((?!})[\s\S])*) "
| Note that's VB.NET syntax, you'd have to ensure that works with the
| VBScript regular expression engine.
| In VB.NET the usage would be:
|
| Dim output = Regex.Replace(strIn, pattern, " characters ")
|
|
|
|
|

| >


| >>


| >> > 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
| >> >
| >
| > As a supplement to my response I attempted to write out an expression to
| > solve this.
| > The case where one wants to capture a named group between two identical
| > chars - double quotes (e.g., " this    stuff is      safe"), is easy -
but
| > I
| > find I'm stumbling over defining a group that is encased with two
separate
| > chars "{" and "}".
| >
| > So if anyone would like to provide a solution it would help to preserve
| > what
| > little hair I have left. <grin>
| >
| > -ralph
| >
| >



Thu, 02 Feb 2012 13:28:24 GMT  
 Replace Space
Here is a fairly short function that should do what you want...

Function ReplaceSpaces(S As String, Replacement As String) As String
  Dim X As Long
  Dim Parts() As String
  ReplaceSpaces = Replace(S, "}", Chr$(0) & "{")
  Parts = Split(ReplaceSpaces, "{")
  For X = 0 To UBound(Parts) Step 2
    Parts(X) = Replace(Parts(X), " ", Replacement)
  Next
  ReplaceSpaces = Join(Parts, "{")
  ReplaceSpaces = Replace(ReplaceSpaces, Chr$(0) & "{", "}")
End Function

Just pass it a text string and the string of characters you want to replace
the spaces with and it will  do that only for the text not included between
the curly braces. For example...

MyText = "abc def ghi {jkl mno pqr} stu vwx yz {123 456 789} abc"
ReplacementText = " X "
Debug.Print ReplaceSpaces(MyText, ReplacementText)

and the code will print out this...

 abc X def X ghi X {jkl mno pqr} X stu X vwx X yz X {123 456 789} X abc

--
Rick (MVP - Excel)


Quote:
>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.



Thu, 02 Feb 2012 13:39:35 GMT  
 Replace Space

Quote:

> Code works.  Thanks!
> What is Const rpl = "&nbsp "
> What does &nbsp stand for?  non-beaking space?
> Litteral insertion?
> Minor point.  Gaining knowledge.

&nbsp doesn't stand for anything.

However, &nbsp; is a non-breaking space.



Thu, 02 Feb 2012 16:13:22 GMT  
 
 [ 21 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Replacing spaces

2. Replace spaces with + in strings

3. Replace spaces with text? HELP

4. Space Space Space Space Space Space Space

5. Space Space Space Space Space Space Space

6. Space Space Space Space Space Space Space

7. Replacing digits[space] with digits[non-breaking space]

8. Search/Replace Em Space/En Space characters

9. How to remove and replace spaces in a web submission form.

10. How to replace spaces in the text area input of a form?

11. HELP! Word Document - Tabs..(Replace with spaces)

12. Replace Break lines with spaces in a string

 

 
Powered by phpBB® Forum Software