.NET, C#, Tips

IEnumerable to Comma Delimited String

A few years ago I blogged about how to take a delimited string and convert it to a IEnumerable.  Today we ran across the opposite of this in that we wanted to take a list and convert it to a delimited string.   This can come in handy for a number of situations so it’s worth taking a look.


List<int> values = new List<int> {10, 20, 25};

In older versions of .NET (prior to 4.0 I believe) the easiest way was to do something like the following:

string newString = string.Join(",", values.ToArray());

However, starting in 4.0, the Join has some additional overloads that make this just crazy simple:

string newString = string.Join(",", values);

If you have a list of strings, there is still some benefit to converting this to an array. There is another overload of Join that allows you to be more selective in what you are converting.

Given a list of strings like the following:

List<string> strings = new List<string> {"a", "b", "c", "d", "e", "f"};

We can look at the last overload of; String.Join(delimiter, string[], startIndex, count)

This gives us a delimited string, starting at the element in the array at startIndex and going for the number of count.

string delimited = string.Join(",", strings.ToArray(), 2, 2);

will return “c,d”

I love how the framework provides these methods, often with multiple overloads to give us a lot of power with very little code. Enjoy!

.NET, Code

String.Contains looking for an empty string

Here’s another one of those little idiosyncrasies that I run across in coding.  In some ways I’m almost embarrassed to say I never knew this as it’s really quite simple and also well documented, but it’s one of those edge cases that just don’t happen that often and may not be that damaging of a bug if it does sneak through.

The construct I’m talking about is the String.Contains() method.   In .NET it simply returns a true/false on whether the string contains the string parameter passed into it.

string s1 = "This is a nice string";
bool b;
b = s1.Contains("nice"); // returns true;
b = s2.Contains("bad"); // returns false;

All nice and simple. The issue I ran into was when the string I was looking for was an empty string.

b = s1.Contains(""); // returns true

In my application, that requirements were that it should return false. However, this returns true. It is well documented, but not something I really had to deal with before.

Something to keep an eye out for as it could give you some false positives if you’re not careful.

Still learning after all these years.

.NET, Code

ActiveControl to tell me the control gaining focus.

I ran into a situation where I needed to know what control was gaining focus in the Leave() event of another control.  The situation I was working with is as follows;  I have a multi-line text box that when it gains focus I want a listview of potential values to popup below it.  When the textbox loses focus, the listview should be hidden.   The user should be able to click on the listview to select items to input into the textbox.  The problem was when the user clicked on the listview, the textbox lost focus and was hiding the listview.

Enter the ActiveControl property on the form.  This handy property allows me to check, in the Leave() event, what is the new active control with focus.  Thus I can check to see if the textbox lost the focus to the listview control and if so, skip the hiding of it.

Here we have the textbox named txtComplaint, when it gets the focus we make listView visible.

private void txtComplaint_Enter(object sender, System.EventArgs e)
  listView.Visible = true;

When the textbox loses focus we see if the ActiveControl is the listView, if not we hide it.

private void txtComplaint_Leave(object sender, System.EventArgs e)
  if (ActiveControl.Name != "listView")
    treeComplaint.Visible = false;
.NET, Code

Delimited String to Generic List<>

This is something that I tend to forget even though it’s pretty basic and probably obvious to most.  I have a comma delimited string which I wanted to put into a List<>. Here’s a one-liner to get the job done.

List aList = new List();

Where myString is my comma-delimited string.  Pretty basic, but very handy.