© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
40 replies
jordinho

❔ Checking if a string only contains specified characters (Order doesn't matter)

I am trying to validate user input. My current approach is to iterate over every char of the input string and check wether the char has been specified. Is there any more efficient way?

class Navigation
    {
        public string Input { get; }

        private List<char> _allowedCharacters { get; }

        public Navigation()
        {
            AllowedCharacters = new List<char>() 
            { 
                '*', '/', '+', '-',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };
        }

        public void UserInput()
        {
            bool invalidInput = true;

            while (invalidInput)
            {                
                Console.Write(">>> ");
                Input = Console.ReadLine();

                for (int i = 0; i < Input.Length; i++)
                {
                    if (_allowedCharacters .Contains(Input[i]))
                    {
                       if (i ==  Input.Length - 1)
                        {
                            invalidInput = false;
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid\n");
                        invalidInput = true;
                        break;
                    }                     
                }      
            }           
        }
    }
class Navigation
    {
        public string Input { get; }

        private List<char> _allowedCharacters { get; }

        public Navigation()
        {
            AllowedCharacters = new List<char>() 
            { 
                '*', '/', '+', '-',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };
        }

        public void UserInput()
        {
            bool invalidInput = true;

            while (invalidInput)
            {                
                Console.Write(">>> ");
                Input = Console.ReadLine();

                for (int i = 0; i < Input.Length; i++)
                {
                    if (_allowedCharacters .Contains(Input[i]))
                    {
                       if (i ==  Input.Length - 1)
                        {
                            invalidInput = false;
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid\n");
                        invalidInput = true;
                        break;
                    }                     
                }      
            }           
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

way to check if a string contains only alphabet
C#CC# / help
2y ago
how do i check if a string contains only words and space
C#CC# / help
2y ago
Check if a string contains a word in an array
C#CC# / help
4y ago
Remove string on specified character
C#CC# / help
4y ago