Solved

C# - How to remove duplicates from a string

Posted on 2014-10-29
4
376 Views
Last Modified: 2014-10-29
Hi..
I have a string of email addreses with duplicates like this.
How can I remove the dups.. maybe use Split and LINQ?

"jon@abc.com; mary@xyz.com ; jon@ABC.com;  MARY@xyZ.com"

I want to return
"jon@abc.com; mary@xyz.com"

thx
0
Comment
Question by:JElster
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 300 total points
ID: 40412149
Give this a shot:

            var dupes = "jon@abc.com; mary@xyz.com ; jon@ABC.com;  MARY@xyZ.com";
            dupes = string.Join(";", dupes.Split(new char[] { ';'}, StringSplitOptions.RemoveEmptyEntries).Select(d=> d.ToLower().Trim()).Distinct());

Open in new window

0
 
LVL 15

Assisted Solution

by:David L. Hansen
David L. Hansen earned 200 total points
ID: 40412157
Sure.  When you use the Split function the result is an array of strings (for you just use the semicolon as the delimiter)...like this:
string[] emails = yourEmailList.Split(';');
string[] uniqueEmails = emails.Distinct().ToArray();

Open in new window

0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 40412172
You have to convert to lowercase as per my example, otherwise it's not going to trim out the case differences.
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 40412185
Yes, in my code just add a ".ToLower" right inbetween "yourEmailList" and "Split".  Thanks Snarf.
string[] emails = yourEmailList.ToLower.Split(';');

Open in new window

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

930 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

12 Experts available now in Live!

Get 1:1 Help Now