Solved

Take the spaces out of this string after the comma.

Posted on 2011-09-05
10
338 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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