0

I have a method like this

public static List<string> ToList(this string str)
{
    return str.Split(',').ToList();
}

I want to know if I can keep the 1-line beauty and also check if the item in the split is actually a string without any whitespaces or a whitespace itself, so basically if its a solid word without and spaces.

1
  • 1
    ok. so if you check it....and its whitespace, then what? you can check that with linq...but ok...so what then? you have to either remove them or something, and that can be done w/linq too...you just have to add onto your 1 liner chain... Commented Mar 25, 2018 at 5:56

2 Answers 2

1

How about this code block?

public static List<string> ToList(this string str)
{
    return str.Split(',').Where(vstr => !string.IsNullOrWhiteSpace(vstr)).Distinct().ToList();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note - The above code has a distinct clause added, which will remove duplicates.
1

You must use a where statement from LINQ.

public static List<string> ToList(this string str)
{
    return str.Split(',').Where( s=> !string.IsNullOrWhiteSpace(s)).ToList();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.