• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

Take the spaces out of this string after the comma.

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
MikeMCSD
Asked:
MikeMCSD
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
X_layerCommented:
string v = s.Replace(", ", ",");

Open in new window

This will replace all commas+space with commas only, in given string.
0
 
disruptCommented:
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
 
MikeMCSDAuthor Commented:
thanks . . is it possible to add it to the replace I have already?

 p.MetaKeywords.Replace('"', '\''), ", . . .
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Kusala WijayasenaSoftware EngineerCommented:
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
 
disruptCommented:
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
 
disruptCommented:
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
 
X_layerCommented:
I don't know but you can try:
(p.MetaKeywords.Replace('"', '\'')).Replace(", ", ",")

Open in new window

0
 
disruptCommented:
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
 
Kusala WijayasenaSoftware EngineerCommented:
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
 
MikeMCSDAuthor Commented:
thanks all
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now