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

Take the spaces out of this string after the comma.

Posted on 2011-09-05
10
335 Views
Last Modified: 2012-05-12
In the field "MetaKeywords" below :

sb.Append(string.Format("{0}{1}", p.MetaKeywords.Replace('"', '\''), ",Home Alarms"));

it has a value like : "dome cam, dummy cam, fake Surveillance, fake security, cameras"


I want to remove the space after the commas, like : "dome cam,dummy cam,fake Surveillance,fake security,cameras"


How can this be done in C#? thanks
0
Comment
Question by:MikeMCSD
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 11

Accepted Solution

by:
X_layer earned 167 total points
ID: 36485747
string v = s.Replace(", ", ",");

Open in new window

This will replace all commas+space with commas only, in given string.
0
 
LVL 16

Assisted Solution

by:disrupt
disrupt earned 167 total points
ID: 36485800
you could just do .Replace(", " ",")

and if you are worried about multiple spaces you can use something like this:

http://stackoverflow.com/questions/206717/how-do-i-replace-multiple-spaces-with-a-single-space-in-c

to get it down to a single space then use:

you could just do .Replace(", " ",")
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 36485806
thanks . . is it possible to add it to the replace I have already?

 p.MetaKeywords.Replace('"', '\''), ", . . .
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 11

Assisted Solution

by:Kusala Wijayasena
Kusala Wijayasena earned 166 total points
ID: 36485828
Check this out

using System.Text.RegularExpressions;
..............................................

string pattern = @"\s*,\s*";
string input = "dome cam, dummy cam, fake Surveillance, fake security, cameras";
string replacement = ",";
            
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

Open in new window


-Kusala
0
 
LVL 16

Expert Comment

by:disrupt
ID: 36485829
so it would be something like so:


string s = "dome cam,     dummy cam, fake Surveillance, fake security, cameras";

            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"[ ]{2,}", options);
            string x = regex.Replace(s, @" ");


            Response.Write(x.Replace(", ", ","));

Open in new window

0
 
LVL 16

Expert Comment

by:disrupt
ID: 36485835
yes to add in your replace just do:
string s = "dome cam,     dummy cam, fake Surveillance, fake security, cameras";

            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"[ ]{2,}", options);
            string x = regex.Replace(s, @" ");


            Response.Write(x.Replace(", ", ",").Replace('"', '\''));

Open in new window

0
 
LVL 11

Expert Comment

by:X_layer
ID: 36485836
I don't know but you can try:
(p.MetaKeywords.Replace('"', '\'')).Replace(", ", ",")

Open in new window

0
 
LVL 16

Expert Comment

by:disrupt
ID: 36485844
so it would be like so:
RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"[ ]{2,}", options);
            string x = regex.Replace(p.MetaKeywords, @" ").Replace(", ", ",").Replace('"', '\'');

            sb.Append(string.Format("{0}{1}", x, ",Home Alarms"));

Open in new window

0
 
LVL 11

Expert Comment

by:Kusala Wijayasena
ID: 36485881
if you exactly wants to replace "comma with white space " by "comma" then do it as follows

sb.Append(string.Format("{0}{1}", p.MetaKeywords.Replace('"', '\'').Replace(", ", ","), ",Home Alarms"));

Open in new window


-Kusala
0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 36485889
thanks all
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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