Quantcast
Channel: Visual Studio and tools – The Wiert Corner – irregular stream of stuff
Viewing all articles
Browse latest Browse all 239

.NET/C# LINQ gem: How to check if a char in a list of characters (via Stack Overflow)

$
0
0

Often when comparing characters with a list of characters, that list does not consist of consts.

It if were, you could make a switch statement:

                    switch (item)
                    {
                        case '/':
                        case '\\':
                        case ';':
                            addSeparator(separatorsUsed, item);
                            break;
                    }

But reality is that you cannot do things like this:

                    switch (item)
                    {
                        case Path.AltDirectorySeparatorChar: // Error: A constant value is expected
                        case Path.DirectorySeparatorChar:
                        case Path.PathSeparator:
                            addSeparator(separatorsUsed, item);
                            break;
                    }

However, you can perform a small trick and use LINQ to write some pretty elegant code based on Contains.

                    char[] pathSeparators = { Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar, Path.PathSeparator };
                    // LINQ: http://stackoverflow.com/questions/1818611/how-to-check-if-a-char-in-a-char-array/1818635#1818635
                    if (pathSeparators.Contains(item))
                        addSeparator(separatorsUsed, item);

The LINQ logic has the logic backwards (you can think of it like “item in pathSeparators”, but it is far easier to read than this:

                    if ((item == Path.AltDirectorySeparatorChar) || (item == Path.DirectorySeparatorChar) || (item == Path.PathSeparator))
                        addSeparator(separatoseparatorsUsedrsInUse, item);

Full source of a demo application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading.Tasks;

namespace CharIsInSetConsoleApplication
{
    class Program
    {
        /*
         * call with arguments like this:
         *     ftp://besharp.com;C:\Windows\system32\cmd.exe 
         */
        static void Main(string[] args)
        {
            List<char> separatorsUsed = getSeparatorsUsed(args);

            Console.WriteLine("Separators used in args:");
            // regular foreach is more readable, but you can use LINQ here too: 
            // http://stackoverflow.com/questions/823532/apply-function-to-all-elements-of-collection-through-linq/823563#823563
            separatorsUsed.ForEach(item => 
                Console.WriteLine(item)
            );
            Console.Write("Press <Enter>");
            Console.ReadLine();
        }

        private static List<char> getSeparatorsUsed(string[] args)
        {
            char[] pathSeparators = { Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar, Path.PathSeparator };
            List<char> separatorsUsed = new List<char>();
            foreach (string arg in args)
            {
                foreach (char item in arg)
                {
                    switch (item)
                    {
                        case '/':
                        case '\\':
                        case ';':
                            addSeparator(separatorsUsed, item);
                            break;
                    }

                    //switch (item)
                    //{
                    //    case Path.AltDirectorySeparatorChar: // Error: A constant value is expected
                    //    case Path.DirectorySeparatorChar:
                    //    case Path.PathSeparator:
                    //        addSeparator(separatorsUsed, item);
                    //        break;
                    //}

                    if ((item == Path.AltDirectorySeparatorChar) || (item == Path.DirectorySeparatorChar) || (item == Path.PathSeparator))
                        addSeparator(separatorsUsed, item);

                    // LINQ: http://stackoverflow.com/questions/1818611/how-to-check-if-a-char-in-a-char-array/1818635#1818635
                    if (pathSeparators.Contains(item))
                        addSeparator(separatorsUsed, item);
                }
            }
            return separatorsUsed;
        }

        private static void addSeparator(List<Char> separatorsUsed, char item)
        {
            if (!separatorsUsed.Contains(item))
                separatorsUsed.Add(item);
        }
    }
}

–jeroen

via: c# – How to check if a char in a char array – Stack Overflow.


Filed under: .NET, .NET 3.5, .NET 4.5, C#, C# 3.0, C# 4.0, C# 5.0, Development, LINQ, Software Development, Visual Studio 11, Visual Studio 2008, Visual Studio 2010, Visual Studio and tools

Viewing all articles
Browse latest Browse all 239

Trending Articles