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!

How to remove extra space in a string

Status
Not open for further replies.

Jeff98

Programmer
Oct 19, 2006
14
US
__mystr___

how to remove the extra space before and after mystr?

THX
 
Replace(mystrg," ","")

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Another way (preserving embedded spaces):
Trim(myStr)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi Jeff98,

As PHV suggests, you can use Trim(MyStr), but be aware this will affect double spacings within the string too. Same goes for the solution posted by missinglinq. For example:
" My String "
gets changed to:
"My String"
If preserving any internal double spacing is important, you could achieve that via:
LTrim(RTrim(MyString))

Cheers

[MS MVP - Word]
 
macropod, which VBA has a Trim function affecting double spacings within the string ?
 
PHV is correct! The QuickBasic4.5 (which VB and VBA was based on) LTRIM and RTRIM removes spaces from either end of the string, preserving "internal" spacing. TRIM simply combines these two functions! The caveat about my answer is valid.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Indeed we are! I was just pointing out the probable source of the confusion - and it could be used in VBA.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Perhaps another tear in the Space Time Continuem?

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Hi PHV,

I had the vba & Excel Trim functions confused ....

Cheers

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top