Solved

Removing Spaces from a string

Posted on 2003-10-29
7
1,410 Views
Last Modified: 2010-04-17
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 -------
0
Comment
Question by:gideonn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9642760
what language?

e.g.,

vb: Trim("John Doe        ");

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

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

   
0
 
LVL 1

Author Comment

by:gideonn
ID: 9642782
Ops My bad I thought I was in the VB forum..  VB Thanks Bobbit!
0
 
LVL 2

Accepted Solution

by:
MattWare earned 250 total points
ID: 9643086
strSpacelessString = RTrim(strString1)+LTrim(strString2)
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 18

Expert Comment

by:bobbit31
ID: 9643287
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)
0
 
LVL 1

Author Comment

by:gideonn
ID: 9643428
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.
0
 

Expert Comment

by:vkgonline
ID: 9656900
It's easy.
use Trim command to remove extra spaces from the string.
Syntax:

newStr=trim(oldStr)

Vinod
0
 

Expert Comment

by:danessin
ID: 9664956
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
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question