Hi,

This is vaguely related to my last post, but not entirely so I'll make a new one. Hope that's consistent with the board rules :)

I have a path

String path = "C:\\test\another\\directory\\";

It seems the trailing slash is appended automatically when using the OpenFileDialog, so I used to in the initialization of the variable too.

Now, in order to go down one directory when the user double clicks the ".." in my ListBox, I (after a good nudge in the right direction) split the parts of the string up using

string[] subPaths = path.Split('\\');

Now, here's the problem:
With this split, I end up with an empty string in the array (the last one). This is ok as I know, the way I implemented it, that it will always be an empty string there, but:
1. I hate leaving stuff that could introduce bugs
2. I hate "just leaving it as it is because it's comfortable".

So, the question is this:
1. Is it possible to tell Split to leave out empty strings?
2. Is it possible to cast the outcome of Split to an ArrayList, and then check the last string and delete it if it's empty?

Cheers!

Recommended Answers

All 11 Replies

Just a small addition...I tried

ArrayList arrayPaths = (ArrayList)path.Split('\\');

but it failed with "Cannot convert type 'string[]' to 'System.Collections.ArrayList'"...

Hello, tiwas.
Split method has a few overloads. Take a look: String.Split Method

Instead of using ArrayList I would suggest you look at List<T>. It has a constructor, that can take a IEnumerable<T> as parameter, which is (in your case) the data, which Split method returns.

Try this :

string path = "C:\\test\\another\\directory\\";
            // because the split method splits on every "splitter char"
            // when it comes to the end(which in this case is a "splitter char", 
            // it will split of an empty string so remove the end of the string
            path = path.Remove(path.Length - 1); // remove last 2 chars
            
            // the Split method returns a string[] 
            // this is not the same as the typeless ArrayList your are trying to cast to.
            string[] subPaths = path.Split('\\');
            
            // a better way(as Antenka suggested is to use a typed list
            List<string> SubPaths = new List<string>();
            SubPaths.AddRange(path.Split('\\'));

Just use path.Split('\\', StringSplitOptions.RemoveEmptyEntries) .

Thanks all! I will look into all of your answers - my objective isn't to find the most convenient way of doing it, but to learn as much as possible :)

Cheers!

Just use path.Split('\\', StringSplitOptions.RemoveEmptyEntries) .

This did not compile on my machine... so I modified it somewhat:

string path = "C:\\test\another\\directory\\";
            char[] charSeparators = { '\\' };
            string[] subPaths = path.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);

Could it be because '\\' does not look like a char?
Apparently it behaves like one in some cases but not in all.

Edit: of course there is different behavior, a char is not a char[] !

Whoops, I meant path.Split(new[] { '\\' }, StringSplitOptions.RemoveEmptyEntries) .

And if you wanted to filter the empty entries manually, you'd probably do path.Split('\\').Where(s => s.Length > 0).ToArray()

Thanks! I tried the StringSplitOptions, but I wasn't aware that I needed a char[] instead of my char. Changing it to a char[] was the key! :D

This is the best way to learn. Maybe I should write a book called "Learn by 'Ooops!' - C#" :p

Hope you guys don't mind :)

path.Split('\\').Where(s => s.Length > 0).ToArray()

I like the sound of that, but I'm afraid it's way beyond my level. Mind explaining what it does? :)

I like the sound of that, but I'm afraid it's way beyond my level. Mind explaining what it does? :)

It uses the function System.Linq.Enumerable.Where, which takes an IEnumerable<T> and a function of type Func<T, Bool> and returns an IEnumerable<T> that contains all the values for which the function returns true.

Since it's an extension method, you can write path.Split('\\').Where(s => s.Length > 0) , which is equivalent to Enumerable.Where(path.Split('\\'), s => s.Length > 0) . The expression s => s.Length > 0 here is a function that takes a string and returns a boolean.

You could implement it like this

public static class Enumerable {
// pardon my indentation
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, Bool> predicate)
{
    foreach (T element in source)
    {
        if (predicate(element))
        {
            yield return element;
        }
    }
}

}

And calling that function doesn't evaluate the interior -- it returns an IEnumerable<T> such that when you enumerate it, the interior is evaluated step by step.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.