I am reading a file data as follows:
string cellData = _reader.GetField("name").ToLower();
I would like to check if cellData is not equal to true or false as string.
I have tried the following:
if (cellData != "true" || cellData != "false") { }
The problem with the above code will always return true.
so what I did I created an array and then used contains:
string[] boolValues = { "true", "false" };
if (!boolValues.Contains(cellData)) { //error }
is it possible to check using c# operator if the string does not contains the word true or false than return error if it contains one of them that it should not return an error using single line if statement
CodePudding user response:
One of the most efficient ways would be:
var hashSet = new HashSet<string>(StringComparer.OrdignalIgnoreCase);
hashSet.Add(Boolean.TrueString);
hashSet.Add(Boolean.FalseString);
if (hashSet.Contains(cellData))
{
}
or
if (String.Equal(cellData, Boolean.TrueString, StringComparison.OrdinalIgnoreCase) ||
String.Equal(cellData, Boolean.FalseString, StringComparison.OrdinalIgnoreCase))
{
}
CodePudding user response:
You can fix the code that you wrote in this way, but it will only check if the file is with one word?!
string[] boolValues = { "Hello", "stack", "true", "false" };
if (!boolValues.Contains(cellData))
{ //error; need using System.Linq;
Console.WriteLine("Not exist");
}
else
{
Console.WriteLine("exist");
}
string[] boolValues = { "Hello", "stack", "true", "false" };
if (!boolValues.Contains(cellData))
{ //error; need using System.Linq;
Console.WriteLine("Not exist");
}
else
{
Console.WriteLine("Exist");
}
Or see the other of the previous answers
CodePudding user response:
If your requirement is limited to check for the values "true" and "false", you could possibly TryParse the string to bool. It will parse the string regardless of the case.
string cellData = "TruE"; // "true", "False", "false", "not_bool"
if(!bool.TryParse(cellData, out bool result))
{
"cellData is Not true nor false".Dump();
}
else
{
$"cellData is {result}".Dump();
}
CodePudding user response:
Just to verify, your problem statement is
I want to validate a string such that the string is valid if it is the literal
true
orfalse
so that I can identify errors
Correct?
You could say something like the following.
var isValid = s == "true" || s == "false";
if (isValid )
{
// handle valid case here
}
else
{
// handle invalid case here
}
That is certainly the simplest way. But you should not that this test is case-sensitive, which can cause problems down the line. But whether or not that's an issue is entirely dependent upon your use case.
Note that is good practice to isolate assertions like this into their own variable (the compiler's optimizer is going to optimize it way), giving it a name that expresses your intent. Also prefer positive assertion like isValid
rather than negative assertions like isNotValid
. Because a test like
if ( ! isNotValid ) { ... }
makes little sense (double negative) and is hard to understand, while a test like
if ( ! isValid ) { ... }
makes perfect sense and is easy to understand.
Another way to do it is to use a regular expression:
var rxTrueFalse = new Regex(@"^(true|false)$");
var isValid = rxTrueFalse.isMatch(s);
if (isValid )
{
// handle valid case here
}
else
{
// handle invalid case here
}
If you want it to be case-insensitive, the adding the appropriate regex option does the trick:
var rxTrueFalse = new Regex(@"^(true|false)$", RegexOptions.Ignorecase);
CodePudding user response:
public static class StringExtension
{
public static bool EqualsIgn(this string text, string textToCompare)
=> text.Equals(textToCompare, StringComparison.OrdinalIgnoreCase);
public static bool HasEquals(this string text, params string[] textArgs)
{
for (int index = 0; index < textArgs.Length; index )
{
if (text.EqualsIgn(textArgs[index])) { return true; }
}
return false;
}
public static bool HasNoEquals(this string text, params string[] textArgs)
=> text.HasEquals(textArgs) == false;
}
public class Program
{
public static void Main(string[] args)
{
if (cellData.HasNoEquals("true", "false"))
{
// code here
}
}
}