Solved

Take the spaces out of this string after the comma.

Posted on 2011-09-05
10
336 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
[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
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Formatting DateTime String Value 5 50
Need help converting bitmap to image in VB.Net 8 45
Making exceptions for ValidationAttribute 2 25
c#, case, if 4 18
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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