Link to home
Start Free TrialLog in
Avatar of turney2
turney2

asked on

getting rid of excess white spaces in a text file

Dear Experts,
I am new to Visual Basic. I am trying
to write a function that will reformat any text file that has multiple white spaces (See below) to a new string that only has one white space between each word., for example:

string: I     don't         know this

when excess spaces are removed would be:
string: I don't know this

In other words, I want to limit the spaces between each word to only one white space between each word. Will someone help me please ?

Turney Brown
ASKER CERTIFIED SOLUTION
Avatar of raquel061097
raquel061097

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of abaldwin
abaldwin

Hey raquel,,,,,where did you get the replace function.  Is that a VB6 function or a VB Script function?????

Just wondering  would like to make use of it..

Andy
If you have VB6, try this..

<----- Code Begin ----->

Public Function xReplaceExtraSpaces (byval strText as String) _
As String

  If Len(strText) > 0 _
  Then
    Do Until InStr(1, strText, "  ") = 0
        strText = Replace(strText, "  ", " ")
    Loop
  End If

  xReplaceExtraSpaces = strText

End Function

<----- Code End ----->

By the way.. in Raquel's example.. don't forget to test for a zero length string.. <smile>
Abaldwin:
No magic about it.. both examples above were written from scratch.. <smile>.
How is the performance of that method, wsh2/raquel?

Here's a method that does not repeatly go thru the same string over and over again, and it uses a API (yikes!)..

The performance of this method is greatly improved using the 'advanced settings' when you compile it, removing (unnneccesary) stuff like array bounds checks and integer overflow. ;)


Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Function RemoveSpc(Value As String) As String

  Dim a As Integer
  Dim LastSpc As Integer
  Dim RemoveChars As Integer
  Dim ByteArray() As Byte
 
  If (Len(Value) = 0) Then Exit Function
 
  ReDim ByteArray(0 To Len(Value) - 1)
  Call CopyMem(ByteArray(0), ByVal Value, Len(Value))
 
  For a = 0 To (Len(Value) - 1)
    If (ByteArray(a) = 32) Then
      If (LastSpc = a - 1) Then
        RemoveChars = RemoveChars + 1
      ElseIf (RemoveChars > 0) Then
        ByteArray(a - RemoveChars) = ByteArray(a)
      End If
      LastSpc = a
    ElseIf (RemoveChars > 0) Then
      ByteArray(a - RemoveChars) = ByteArray(a)
    End If
  Next
 
  If (RemoveChars = 0) Then
    RemoveSpc = Value
  Else
    RemoveSpc = Space$(Len(Value) - RemoveChars)
    Call CopyMem(ByVal RemoveSpc, ByteArray(0), Len(RemoveSpc))
  End If
 
End Function
Oh and I forgot.. this method does work in VB5 too. ;)
Avatar of hes
Abaldwin:
Replace is a vb6 addition
Very nice Vbmaster.. particularily the string to byte array copymem cast.. <smile>.. but.. mine only took a minute to write.. LOL and a <wink>.

One performance note to both the Raquel and WSH2 example.. the Instr comands shoud be changed to Instr$. Although, it will not create the performance gains of Vbmaster's solution.. every little bit helps.. <smile>.

One last note.. Vbmaster's comment to the VB Advanced Optimization settings (Project Properties.. Compile.. Advanced optimizations) is a good one.. if you are absolutely certain there will be no overflow / exceeded boundary conditions.. then go with the wind.. <smile>
abaldwin, here is what I gave turney2.

Function FcnRemoveWhiteSpace(strLine As String) As String
    Dim intPlace As Integer, strNewLine As String
    strNewLine = strLine
    Do
        intPlace = InStr(1, strNewLine, "  ")
        If intPlace = 0 Then Exit Do
        strNewLine = Replace(strNewLine, "  ", " ")
    Loop
    FcnRemoveWhiteSpace = strNewLine
End Function

Although vb2's answer is more streamlined. Good job vb2. I believe any of the answers will work in VB4-6. VBMaster, whoah, cool. I'll have to run all these and check the performances on larger strings. I do believe VBMasters is the fastest though.
Raquel:
As indicated earlier.. the REPLACE function I used is VB6 specific. Yours and Vbmasters are varsion independent. As to the performance issues brought up by Vbmaster, the speed of the processor, the size of the text to be converted, and how fast can the storage devices handle I/O.. are additional determinants in whether application code is percieved to be fast or or to be slow. While it is always nice to optimize your code where possible.. it doesn't always mean you will get an equivalent performance payback.. as there can be other factors involved.. <sigh>.

wsh2: okay, my code took me a minute or two more than that. But I can't understand what Instr$ would do? I guess you were thinking about Mid($) and stuff like that. ;)
Vbmaster:
Yes.. Instr$ is employed the same as Mid$.. <smile>
There are replacements for Replace (which is only available in VB6) on the web.

Try www.vbthunder.com or www.vbwire.com
Avatar of turney2

ASKER

Hey Raquel,
This is excellent. I does the very well.
Much thanks,

Turney Brown
Avatar of turney2

ASKER

Hey Raquel,
This is excellent. It does the job very well.
Much thanks,

Turney Brown