Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Removing Spaces from a string

Posted on 2003-10-29
7
Medium Priority
?
1,412 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 1000 total points
ID: 9643086
strSpacelessString = RTrim(strString1)+LTrim(strString2)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

670 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