Solved

Need to modify a text field with commas.

Posted on 2011-09-05
4
318 Views
Last Modified: 2012-05-12
I have a field "MetaKeywords" that contains values separated by commas, like:

"dome cam, dummy cam, fake Surveillance, fake security, spy, cams, cameras"

I need to do 2 things to it:

1. Remove the space after the commas, like : "dome cam,dummy cam,fake........ "

2. Limit the field to just 5 values separated by commas, like : "dome cam, dummy cam, fake Surveillance, fake security, spy"

How can this be done in C#? thanks
0
Comment
Question by:MikeMCSD
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36484662
Try this:
string value = "dome cam, dummy cam, fake Surveillance, fake security, spy, cams, cameras";
string[] parts = value.Split(',');

for (int i = 0; i < parts.Length; i++)
{
    if (i < 5)
    {
        parts[i] = parts[i].Trim();
    }
    else
    {
        parts[i] = string.Empty;
    }
}

string result = string.Join(",", parts).TrimEnd(',');

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36484682
You could also do a extension-method-based approach:
using System.Linq;

...

string value = "dome cam, dummy cam, fake Surveillance, fake security, spy, cams, cameras";

string result = value.Split(',')
                     .Take(5)
                     .Aggregate((accum, curr) => accum += curr.Trim() + ",").TrimEnd(',')

Open in new window

0
 
LVL 16

Author Comment

by:MikeMCSD
ID: 36484750
Nicely done!  Thanks kauf
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36485174
NP. Glad to help  = )
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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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