473,480 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Remove spaces

Hello,

I have the following string:

str = " string , hello,something ,new object, london city "

How can I remove all the spaces before and after a comma but never the
ones between words?

So I get my string as follows:
str = "string,hello,something,new object,london city"

Thanks,

Miguel

Apr 30 '07 #1
1 1255
"shapper" <md*****@gmail.comwrote in message
news:11*********************@l77g2000hsb.googlegro ups.com...
I have the following string:

str = " string , hello,something ,new object, london city "

How can I remove all the spaces before and after a comma but never the
ones between words?

So I get my string as follows:
str = "string,hello,something,new object,london city"
I'm sure there are much cleverer and more efficient ways of doing this,
maybe with Regular Expressions, but the following will do it for you:

string str = " string , hello,something ,new object, london city ";
while (str.Contains(" ,"))
{
str = str.Replace(" ,", ",");
}
while (str.Contains(", "))
{
str = str.Replace(", ", ",");
}
str = str.Trim();

--
http://www.markrae.net

Apr 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
89776
by: Thomas Mlynarczyk | last post by:
Which is the simplest way to remove all whitespace from a string? Is there a simpler method than a regex replace? Or how can I tell a regex pattern to ignore all whitespace in my subject string?...
2
9196
by: john | last post by:
I am using this to remove everything except the 0-9 and a-z characters, but I also want to remove blanks spaces as well(yes even ones in between words) $strippedtext =...
1
4044
by: Danny | last post by:
Given this: "*" the pattern to remove the htnl code between brackets, how can I remove a series of spaces and just make one space. like here is a series of blank...
2
2827
by: BobM | last post by:
I have two strings that I would like to compare. To guarantee the compare is not affected by trailing spaces, I would like to remove them. Will this do: MyTextBox.text.ToString().Trim()?
1
1791
by: Arjen | last post by:
Hello, In code view I use a tab as mark-up character. When I run my web application I can see the tabs with the source viewer in my browser. Is there a way to remove the tabs, extra spaces...
3
7828
by: Danny Yeadon | last post by:
Hi I need to remove unwanted characters from phone numbers from my phone bill to analyse the data. Some examples are 02 48222222 i need to remove the space +61266667656 ...
7
2017
by: Mike Johnson | last post by:
How do I remove a item that's been selected from a listview? I'm using Visual Basic 2005 express edition. Thanks
34
4053
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
15
15593
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
1
2491
by: buggtb | last post by:
Hi Guys, I've been given the joyous task of updating some very old scripts at work and I could do with a little help. Our Unix system dumps text files to pseudo spoolers that our windows...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.