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
 
Look into the replace function instead.

"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'."
 
step through it, use F9 to put a break on the line and then run the program and when it breaks on the line step through using F8, this will allow you to see why it is not finishing the loop...i would test your code but i'm not on my dev machine.
 
Thanks for the tip. I will check into replace.

The questions remains, however. What is wrong with this code?! Why is it exiting prematurely?
 
It's trying to start at position 0. The mid function starts a position 1. You could accomplish the same thing by doing what DrJavaJoe and others recommended:

[code}
stripSpaceChars = replace(strTest," ","")
[/code]

-B
 
Also, it seems to me that the Replace() function won't handle an arbitrary number of consecutive character substitutions. In other words, I would have to code an infinite number of lines of Replace() calls so handle strings with an arbitrary number of space characters in between non-space characters.

Did I miss something?
 

First problem is the For I = 0 To... since there is no character at position zero, meaning an error will/should occure at If Mid(snew, i , 1) = " "...

Fix that and see what happens

BTW, have you read FAQ222-2244 yet?

Good Luck

 
Use the instr function to make sure you have rid yourself of any double spaces.

Do while instr(TheString, " ") <> 0
TheString = replace (TheString," ", " ")
Loop

&quot;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'.&quot;
 

from MSDN:
[tt]
Replace returns the following values:

If

expression is zero-length Zero-length string ("")
expression is Null An error.
find is zero-length Copy of expression.
replace is zero-length Copy of expression with all occurences of find removed.
[/tt]
 
You din't miss anything if you have
TheString = replace("4 spaces", " ", " ")
It will replace the 2 instances of double spaces with a 2 instances of single spaces, so you'll have to check again to replace these.


&quot;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'.&quot;
 
Thanks for the help, gang. It seems I'm used to C-code, and starting at position 0 rather than 1. Starting at position 1 seems to work.

Note: Using the replace() function won't help me, it seems. I'm not looking to replace double spaces with single spaces. I guess this would work eventually, if i keep substituting doubles with singles and then keep substituting the string, but how inefficient is that?!

Is there a built-in VB function (like replace) that would allow VARIABLE-LENGTH substitutions? For instance, is there something like a perl substitution expression:

e.g.
$myString =~ s/\s+//g;

Or (from my imagination) for a VB function:
myString = myReplace(myString," "*," ")

Where " "* means one or more instances of the given character?

 
You can try using the Split function with the trim function to first break up your string and then rebuild it. the following function takes.

"This is a Test for extra spaces "

and returns

"This is a Test for extra spaces"

Code:
Private Function stripSpaceCharacters(s As String) As String
    Dim i As Integer, sNew As String
    Dim myArray() As String
    
    myArray = Split(Trim(s), " ")
    For i = 0 To UBound(myArray)
        If myArray(i) <> "" Then sNew = sNew & Trim(myArray(i)) & " "
    Next i
    stripSpaceCharacters = Trim(sNew)
End Function




zemp
 
>there something like a perl substitution expression

There's something exactly like that. Check out the Microsoft VbScript Regular Expressions library which you should find under Project/References
 
If you use two quotation marks next to each other, the spaces will be replaced with nothing until no more spaces exist.

- B
 
strongm should be along shortly and suggest that you look into RegExp, regular expressions.

&quot;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'.&quot;
 
See.

&quot;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'.&quot;
 

>In other words, I would have to code an infinite number of lines of Replace() calls so handle strings with an arbitrary number of space characters in between non-space characters.

Nah.
Just two calls:

NewString = Replace(TheString, Chr$(32) & Chr$(32), vbNullString)
NewString = Replace(NewString, Chr$(32), vbNullString)
 
Ooo! I like the Trim() + Split() suggestion. Thank you, kindly.

Also, I am interested in using regular expressions. Funky monky. I'll check out the regular expression libary.

Ok, so I'm still confused about VB and 0-indexing. Is 0-indexing used at all in VB? If so, when? When to use 1-indexing?



 

>Nah.
>Just two calls:

Nahhhh... Just one...

[tt]
Option Explicit

Private Sub Form_Load()
MsgBox Replace("This is a test .", " ", "")
End Sub
[/tt]

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top