'How to check if string contains ANY values in the given array AND NOT others values
I wanna make simple string calculator, my point is to check that user typed in any values from the array and not others.
string[] occurences = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "log", "lg", "p", "P", "e", "E", "(", ")" };
string operation = Console.ReadLine();
For instance user typed string like "1+2*(log(5)+1)=" with no whitespaces and it needes to be checked that it doesn't contains unwanted values like word "amogus" or something. The string that user typed should only contains any values from array (it doesn't necessarily should contains all values just any)
Solution 1:[1]
using System.Linq
string[] occurences = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "log", "lg", "p", "P", "e", "E", "(", ")" };
string operation = Console.ReadLine();
//user must add whitespace between each input, i.e whitespace is required and valid input
bool isValidInput = operation.Split(' ').All(c => occurences.Contains(c));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user18636778 |