• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

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
0
turney2
Asked:
turney2
  • 5
  • 3
  • 2
  • +4
1 Solution
 
raquel061097Commented:
This function should work. All it does it replace all double spaces with single spaces. It loops until all double spaces are gone. If there are 4 spaces in a row then the first sweep should reduce it to 2 and the second to 1. Hope this helps.

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
0
 
abaldwinCommented:
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
0
 
wsh2Commented:
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>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
wsh2Commented:
Abaldwin:
No magic about it.. both examples above were written from scratch.. <smile>.
0
 
VbmasterCommented:
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
0
 
VbmasterCommented:
Oh and I forgot.. this method does work in VB5 too. ;)
0
 
hesCommented:
Abaldwin:
Replace is a vb6 addition
0
 
wsh2Commented:
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>
0
 
raquel061097Commented:
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.
0
 
wsh2Commented:
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>.

0
 
VbmasterCommented:
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. ;)
0
 
wsh2Commented:
Vbmaster:
Yes.. Instr$ is employed the same as Mid$.. <smile>
0
 
savvyCommented:
There are replacements for Replace (which is only available in VB6) on the web.

Try www.vbthunder.com or www.vbwire.com
0
 
turney2Author Commented:
Hey Raquel,
This is excellent. I does the very well.
Much thanks,

Turney Brown
0
 
turney2Author Commented:
Hey Raquel,
This is excellent. It does the job very well.
Much thanks,

Turney Brown
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 5
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now