Removing Spaces

MrAustin

Registered User.
Local time
Today, 05:28
Joined
Oct 4, 2004
Messages
32
If I have a long string (most likely 160+ characters) and it is formated like this:

PHP:
100001000024490124336                               040924      HQCWGM       0070158561      00000000000000000  000000000768612C0050123343

How would I, using VBA, remove all spaces from it? I'm attempting to parse the string, so removing the spaces would help greatly in doing so, I think. I'm just unsure as to how to go about it in the most efficient manner. The text file that I will be parsing will have many hundreds of lines such as this.

Thanks!
 
Code:
Public Function RemoveSpaces(ByVal strMessage As String) As String
    Dim intCounter As Integer
    For intCounter = 1 To Len(strMessage)
    If Mid$(strMessage, intCounter, 1) <> Chr(32) Then 
        RemoveSpaces = RemoveSpaces & Mid$(strMessage, intCounter, 1)
    End If
End Function
 
A simpler solution?

I believe that instead of all that recursive code overhead, it may be more efficient to do something like this:

strNewString = Replace(strOriginalString, " ", "")

Regards,
ap_abe
 
ap_abe said:
I believe that instead of all that recursive code overhead, it may be more efficient to do something like this:

strNewString = Replace(strOriginalString, " ", "")

Regards,
ap_abe

Maybe, maybe not, depeding on how Replace was implemented.
 
Either way it'd be examining each character, right? I mean, it's not like it's recursively binary sorting. I figure, for non sorting applications, the built in functions like that will probably be faster than something we can write ourselves, being already (theoretically) optimized.

Anyway, I'd love to see something on just which one is faster -- whichever it is, I'll use that one.
 

Users who are viewing this thread

Back
Top Bottom