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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help deleting spaces in string

Status
Not open for further replies.

bigbaddave15

Technical User
Jan 23, 2003
1
US
I am doing a simple project which detects the number of palindrones in a string. I am having trouble deleting the spaces within a string. I know trim(string) deletes the spaces before and after, but how do you trim spaces between words in a string? Your response is greatly appreciated. Must finish this project in a few days for class. Thanks
 
This should give you the idea:
Code:
  Sub test()
    MsgBox Replace("this is a   string", " ", "")
  End Sub
But, next time look it up in the help file (for example look under "replace")
 
This is something from my collection, it will delete spaces or any other character. Enjoy.

Function Strip(strString As String, strStrip As String)
'Remove any occurence of any character in Strip from String
' Example strip("Hello World", " l") would give "HeoWord"
Dim next_pos As Integer
Dim next_chr As String
Dim new_string As String
new_string = ""
For next_pos = 1 To Len(strString)
next_chr = Mid$(strString, next_pos, 1)
If InStr(1, strStrip, next_chr) = 0 Then
new_string = new_string + next_chr
End If
Next
Strip = new_string
End Function
 
sksmithson: Good add.

bibbaddave15: You coule build on sksmithson's code by replacing the string of characters to strip out with logic to test whether the character is between "a" and "z" and/or between "A" and "Z" to strip out all non-alphabetic characters. Otherwise, in either example you would need to specify what you don't want instead of what you want.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top