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

Trim all spaces from a variable

Status
Not open for further replies.

DoctorGonzo

Programmer
Jan 23, 2004
48
GB
Hi all.

Can anyone tell me how to trim ALL the spaces out of a variable. ltrim, rtrim and trim remove leading and trailing, but I have a space in the middle of a var that just won't go.

I've tried useing replace too. Any ideas?

Many thanks,

Gonzo
 
how about the Replace method/function?

i.e. replace(strX, " ", "")
 
I tried that one Mr Movie, but it leaves the space there. I don't think it should, but it does.

I used varname = replace(varname, " ", "")

Strange...

Are there any other methods?

Many thanks again,

Gonzo
 
you could try the split method based on " " then put the string back together again from the array you get. far too many cpu whatevers, saying that who really knows how the replace function goes about it

the replace method takes some funny parameters to make it carry on replacing i think, rather than just replacing the first one it find
 
Hello DoctorGonzo,

[1] I don't think there is a problem in the functionality of

Code:
    replace(string," ","")

It should perform exactly as one have in mind, ansi or unicode.

[2] The problem might be a "tab" dressed like a "space". Try do one more replace:

Code:
    replace(replace(string," ",""),chr(9),"")

see what you get?

regards - tsuji
 
Cheers guys - it's a bit weird.

Are we allowed to post code samples up here to see if someone can take a peek for me?

I'm sure I'm doing it correctly....

Gonzo
 
DoctorGonzo,

I don't see who will hold one back from posting codes. But, if you are sure you are doing it correctly, maybe, don't bother after all.

- tsuji
 
I agree with tsuji on this one, something other than a space is in your variable. Try doing a replace on vbcr or vbcrlf. msgbox the variable so you can have a good look at how it is being interpreted. Post your code so we can see what the heck is going on. But essentially, if you are doing it right, the space will be removed. So something is still wrong.
 
to stretch the point the original post was 'how to trim ALL the SPACES...' ;-)

 
Hello again,

Replace is replacing _all_ within the string. But, _varname_ is not a "string". That is the problem. If it is really a varname as the name suggested, then you have to do the work from an external script treating the original code like a text file picking out the "varname" string, do the replace, resave the code.

- tsuji
 
Another after-thought. But, can a varname contains a space? Unlikely. So mostly likely, my/our original understanding of the problem posted as such still stands.

- tsuji
 
Just post as snippet from the code you are actually using so we can see where the problem lies.
 
Why not look at the suspect character(s) to verify what it really is? This should display the entire string in ASP
Code:
Dim cInf
cInf = ""
For i = 1 To len(myVar)
   cInf = cInf & cStr(i) & ": " & cStr(asc(mid(myVar,i,1))) & vbCrLf
Next

MsgBox cInf, 48, "View String"

By the way, could it be a hard space, CHR(32+128)?
dbMark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top