Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's wrong with this code?

Status
Not open for further replies.

Quimbly

Programmer
Oct 24, 2002
33
CA

Hello,

I'm new to VB coding. Please take a look at the code below and tell me if you spot any problems. The purpose of this function is to take a string as input and to remove extraneous spaces -- that is, replace multiple instances of space characters with a single space character.

The logic of the code seems fine, however, I think I'm missing something. It seems when I call this function from elsewhere in the project, the code doesn't finish executing. I'm not sure if it's a debugger problem or what, but the code seems to get to the IF statement inside the FOR loop, but then immediately exits.

I have no idea why that would happen.... Any ideas?



Private Function stripSpaceCharacters(s As String) As String
Dim i As Integer
Dim iStart As Integer

Dim sNew As String
sNew = s

For i = 0 To Len(sNew) - 1
If Mid(sNew, i, 1) = " " Then
iStart = i
Do While Mid(sNew, i, 1) = " "
i = i + 1
Loop

sNew = left(sNew, iStart+1) & Mid(sNew, i)
End If
Next

stripSpaceCharacters = sNew

End Function
 
That would return "Thisisatest."

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
Yeah but it wasn't working and he did say The purpose of this function is to take a string as input and to remove extraneous spaces -- that is, replace multiple instances of space characters with a single space character.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 

Well, it is almost midnight here.

Actually I got thrown off by seeing the Split() function, and ran off looking at a piece of code of mine on how to remove empty elements from a one-dim string array, by using a Join(), two Replace() statements, a Split() and a Filter() call.
Time to go to bed.
 

ok then...
[tt]
Option Explicit

Private Sub Form_Load()

MsgBox RemoveDoubleSpaces("This is a test .")

End Sub

Public Function RemoveDoubleSpaces(S As String) As String

Do While InStr(1, S, " ") > 0
S = Replace(S, " ", " ")
Loop
RemoveDoubleSpaces = S
End Function
[/tt]

Good Luck

 
DrJavaJoe is correct. The purpose of the function is to replace multiple instances of the space character with a single space character in a string.

Hence the "iStart+1" in the code, to grab the first space character of the sequence:

sNew = left(sNew, iStart+1) & Mid(sNew, i)

 

Ahh yes I did miss that part as I origionally noticed the coding error but my last post will do exactly what you want.

Good Luck

 
microsoft didn't seem to stick to one standard for indexing, arrays start at 0, but controls like listbox i believe start at 1, you just have to test these things out, strings all start at position 1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top