Link to home
Start Free TrialLog in
Avatar of gideonn
gideonn

asked on

Removing Spaces from a string

Easy, so I have a string, this string could have any number of spaces at its end, and then I have another string, which could have any number of spaces at the beginning.  My question is how can I remove the unwanted spaces so that I have just the data.  Here is an example of what I need...  The number of spaces that are at the end and beginning of the string varies and are not constant...  Thanks!

--------  Example  --------

(String 1)
Before:
String = "John Doe                "

After
String = "John Doe"

(String 2)
Before:
String = "     100"

After:
String = "100"
------- Break -------
Avatar of bobbit31
bobbit31
Flag of United States of America image

what language?

e.g.,

vb: Trim("John Doe        ");

java:
String str = "    100";
str = str.trim();

c++: depends, are you using AnsiString, string, CString, char[] ???

   
Avatar of gideonn
gideonn

ASKER

Ops My bad I thought I was in the VB forum..  VB Thanks Bobbit!
ASKER CERTIFIED SOLUTION
Avatar of MattWare
MattWare

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lol... why didn't i get the points?

strSpacelessString = Trim(str1)

will remove leading and trailing spaces (no need for RTrim, LTrim)

In addition,

In VB the string concat operator is & not +

strSpacelessString = RTrim(strString1) & LTrim(strString2)
Avatar of gideonn

ASKER

Sorry Bobbit I just looked at your post again and the first time I looked at it I didn't see the VB part, I just saw Java and disregarded it, I am sorry please see my next post for your points.
It's easy.
use Trim command to remove extra spaces from the string.
Syntax:

newStr=trim(oldStr)

Vinod
re: In VB the string concat operator is & not +

Either will concatenate strings. The difference is that & is early-bound (i.e. the compiler knows that the reference is a string). + is late bound because the target might be a number, in which case math is required or a string, in which case a concatenation is required. Therefore & is considerably faster than +. Otherwise either will work.

btw - Neither of these solutions will remove multiple spaces between words within the string - in case that's an issue