Solved

getting rid of excess white spaces in a text file

Posted on 2000-03-17
15
376 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now