Solved

C# - How to remove duplicates from a string

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
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.

760 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

17 Experts available now in Live!

Get 1:1 Help Now