Solved

C# - How to remove duplicates from a string

Posted on 2014-10-29
4
397 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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