[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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