Need to modify a text field with commas.

Posted on 2011-09-05
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
Question by:MikeMCSD
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
  • 3
LVL 75

Accepted Solution

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();
        parts[i] = string.Empty;

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

Open in new window

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(',')
                     .Aggregate((accum, curr) => accum += curr.Trim() + ",").TrimEnd(',')

Open in new window

LVL 16

Author Comment

ID: 36484750
Nicely done!  Thanks kauf
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36485174
NP. Glad to help  = )

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

705 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