Solved

Need to modify a text field with commas.

Posted on 2011-09-05
4
314 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Nicely done!  Thanks kauf
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
NP. Glad to help  = )
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now