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
Solved

Removing Spaces from a string

Posted on 2003-10-29
7
1,407 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 105
control image tags in a string ? 12 140
Windows Service to Receive TCP Packets 4 166
VB.net and sql server 4 45
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
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 …

766 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