Solved

getting rid of excess white spaces in a text file

Posted on 2000-03-17
15
384 Views
Last Modified: 2010-05-02
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
Comment
Question by:turney2
  • 5
  • 3
  • 2
  • +4
15 Comments
 

Accepted Solution

by:
raquel061097 earned 50 total points
ID: 2629929
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
 
LVL 4

Expert Comment

by:abaldwin
ID: 2629944
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2629980
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 14

Expert Comment

by:wsh2
ID: 2629984
Abaldwin:
No magic about it.. both examples above were written from scratch.. <smile>.
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2630028
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
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2630029
Oh and I forgot.. this method does work in VB5 too. ;)
0
 
LVL 20

Expert Comment

by:hes
ID: 2630038
Abaldwin:
Replace is a vb6 addition
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2630111
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
 

Expert Comment

by:raquel061097
ID: 2630509
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2630642
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
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2630930
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2630950
Vbmaster:
Yes.. Instr$ is employed the same as Mid$.. <smile>
0
 

Expert Comment

by:savvy
ID: 2631371
There are replacements for Replace (which is only available in VB6) on the web.

Try www.vbthunder.com or www.vbwire.com
0
 

Author Comment

by:turney2
ID: 2631666
Hey Raquel,
This is excellent. I does the very well.
Much thanks,

Turney Brown
0
 

Author Comment

by:turney2
ID: 2631669
Hey Raquel,
This is excellent. It does the job very well.
Much thanks,

Turney Brown
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question