Compare string against a list of strings

S-Soft 666 Reputation points
2023-02-16T11:51:22.6033333+00:00

Hello

Working on a custom storage parser that will save its streams on disk, name is taken from internal stream name.

To make sure it's not of these:

CON, PRN, AUX, NUL, COM0, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT0, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9

What's the fast and short way to store the above strings and compare with my file name to make sure it's not one of them?

vb.net 2010 .net 4.0

Thanks for advise :)

Developer technologies | VB
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. LesHay 7,146 Reputation points
    2023-02-16T13:34:06.3+00:00

    Hi

    Don't know about 'fastest' as I have not tried multiple methods, but I would have tried something like this.

        Dim forbidden() As String = {"CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"}
        Dim filenames() As String = {"lpt5", "com", "com2", "lpt", "aux"}
        Dim hold As String = String.Empty
        For Each filename As String In filenames
          If forbidden.Contains(filename.ToUpper) Then
            hold &= filename & "  "
          End If
        Next
        MessageBox.Show("Forbidden filenames" & vbCrLf & hold)
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.