Trim, LTrim, or RTrim will not remove spaces...

K

kw_uh97

Thanks for reading my question:

I have a string that has spaces in the beginning and/or end of the text,
'examples of the string I encounter are 'some text', ' more text', '
some more text ' and 'even more text '. Now for some reason Trim, LTrim
or RTrim does not remove the spaces in my string. Does any one have a clue
why this will not allow me to remove the spaces?

Thanks In Advance For Any Assistance
kw
 
D

Douglas J. Steele

Are you sure they're spaces, and not some other character?

Try something like the following:

Dim lngLoop As Long

For lngLoop = 1 To Len(strVariable)
Debug.Print "Position " & lngLoop & ": " & _
Mid(strVariable, lngLoop, 1) & " (" & _
Asc(Mid(strVariable, lngLoop, 1)) & ")"
Next lngLoop

Once that runs, go to the Immediate Window. If the blanks don't have (32)
after them, then they're not spaces and you'll need to use the Replace
function to change them to "".
 
K

kw_uh97

k, It looks like they are not spaces. Are these new line and carraige
returns? Id so would the replace would work something like this
Replace(strVariable, char(13), ' ') and Replace(strVariable, char(10), ' ')?

Here is the output of immediate window:
Position 1:
(13)
PositionPosition 5:
(13)
Position 6:
(10)
Position 7:
(13)
Position 8:
(13)
Position 9:
(10)
Position 10:
(13)
Position 11:
(13)
Position 12:
(10)
Position 13:
(13)
Position 14:
(13)
Position 15:
(10)
Position 16:
(13)
Position 17:
(13)
Position 18:
(10)
 
J

John W. Vinson

k, It looks like they are not spaces. Are these new line and carraige
returns? Id so would the replace would work something like this
Replace(strVariable, char(13), ' ') and Replace(strVariable, char(10), ' ')?

Yes; but you can do it in one pass with nested Replaces:

Replace(Replace(strVariable(Chr(13), ""), Chr(10), "")

Note Chr not Char...
 
K

kw_uh97

This works perfectly with just a minor adjustment. You had an extran "(" in
there. :>)

Replace(Replace(strOutput, Chr(13), ""), Chr(10), "")
Replace(Replace(strVariable(Chr(13), ""), Chr(10), "")

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top