Solved

C# - How to remove duplicates from a string

Posted on 2014-10-29
4
449 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
[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
  • 2
  • 2
4 Comments
 
LVL 23

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 23

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

695 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