Solved

Need to modify a text field with commas.

Posted on 2011-09-05
4
322 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
[X]
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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

737 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