Solved

Need to modify a text field with commas.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

21 Experts available now in Live!

Get 1:1 Help Now