Solved

Removing Spaces from a string

Posted on 2003-10-29
7
1,404 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
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now