tags:

views:

72

answers:

3

Hey all, i am trying to replace large spaces between text with just one. My output looks like this right now:

5964215">

        This is just the first example of the spaces
    5964478">

        This would be the 2nd example of showing how many spaces this thing has in each sentence.
    5964494">

That comes from a textbox that has multi-line to true. Here is what it looks like when it doesn't have multi-line to true.

I can not seem to get the spaces to go away! BTW, this text is from a webpage if that makes any difference.

David

+6  A: 

According to the suggestion of MvanGeest, here is some VB code to replace blocks of white spaces:

Sub test()
    Dim x As String, y As String
    x = "abcd       defg             1233"
    Dim re As New RegExp
    re.Pattern = "\s+"
    re.Global = True
    y = re.Replace(x, " ")
    Debug.Print y
End Sub

To make this work, you will have to add a reference to "Microsoft VBScript Regular Expresssions" to your project.

Doc Brown
So there are regexes in VB6, you'll only have to summon them using a secret incantation. Far short of Perl magic, I admit, but wouldn't it be easier to use a newer programming language (@StealthRT)?
MvanGeest
... a newer one like Perl, which is from 1987?
Doc Brown
I meant to say that Perl magic is actually much more dark and frightening than what is being applied here. Nonetheless I am a Perl enthusiast and will make a (probably vain) attempt to defend it by saying Perl's "Stable release" field on WP says 2010 while Visual Basic's reads 1998. I don't mean to start a war over this and think everyone should use the language they like, but also to accept that the older a language is, the more difficult seemingly 'simple' tasks become.
MvanGeest
Oh, and +1 for the solution.
MvanGeest
That did the trick, Doc Brown. Thank you very much :o)
StealthRT
@MvanGeest: thanks for the upvoting, and I should add, I do like Perl more than VB6, too, but I don't like holy wars, either.
Doc Brown
A: 

Assuming no regex support, why not set up a simple state machine that will set the state=1 when a space is found and set state=0 once a non-space is encountered. You can move char by char when state=0 (thus copying over only 1 space per series of spaces).

Edward Leno
Repeating a false myth over and over again does not make it more true.
Doc Brown
A: 

Also assuming no regex, you could try something like

str = "long text with spaces "

i = LenB(str)

str = Replace(str, " ", " ")

Do While LenB(str) <> i

i = LenB(str)

str = Replace(str, " ", " ")

Loop

Of course this code could be optimized for long space sequences but it might be all you need as well

Claudio
The replace is meant to be str, 2 spaces, 1 space
Claudio