Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8111891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:17:29+00:00 2026-06-06T02:17:29+00:00

In this case, the source of the text is from a winforms textbox. I’m

  • 0

In this case, the source of the text is from a winforms textbox. I’m asking this primarily to learn more about LINQ and maybe to demonstrate its strength (easier to read than loops, in my opinion). This program had several requirements; the function had to get the text from the textbox, split it into its separate lines, remove empty and/or duplicate lines, and trim the lines.

Are these the only ways to do this? Are there other methods for this in C# (apart from looping through the list of lines and adding items that meet the criteria to a new list, array, etc.)?

1:

List<String> listOne = textBoxWords.Text
                                   .Split(new char [] { '\r', '\n' })
                                   .Select(s1 => s1.Trim())
                                   .Where(s2 => !String.IsNullOrEmpty(s2))
                                   .Distinct()
                                   .ToList();

2:

List<String> listTwo = textBoxWords.Text
                                   .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                                   .Select(s1 => s1.Trim())
                                   .Distinct()
                                   .ToList();

UPDATE: This code was suggested, but only works if the strings don’t need to be trimmed.

3:

List<String> listThree = textBoxWords.Text
                                     .Split(new char[] { '\r', '\n' },
                                     .Where(s1 => !String.IsNullOrWhiteSpace(s1))
                                     .Distinct()
                                     .ToList();

I know it’s a) fairly specific, and b) could probably be implemented easier using a DataGrid, but the code I’m maintaining uses textboxes and I didn’t immediately want to rewrite them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:17:32+00:00Added an answer on June 6, 2026 at 2:17 am
     var setOne = new HashSet<string>(textBoxWords.Text
                                   .Split(new char [] { '\r', '\n' })
                                   .Select(s1 => s1.Trim())
                                   .Where(s2 => !String.IsNullOrEmpty(s2)));
    

    It’s not exactly the same as what you have since the result is a HashSet, but based on your scenario it looks like that may be the better data structure for what you are trying to accomplish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string read from another source such as \b\bfoo\bx. In this case,
Imagine this case, but with a lot more component buckets and a lot more
This case is a bit complex, I hope I will simplify it well. My
In this case the character is ^ . For example in the following format
In this case, the Visual Studio designer generates a method which takes the parameter
In this case, is it bad to subscribe to the proxy CloseCompleted event? public
Consider this case: public Class1 { public static final String ONE = ABC; public
In this case, I've developed a CSS code for this web application ..and sometimes
I am reading the dragon book. Quoting the text from the book (3.1.4 Lexical
Not sure if anyone has experience this issue. I have a TextBox that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.