dr["DomainName"].ToString().Contains("*")
checks dr["DomainName"]
contains the char *
, but the condition may also be true if it contains other chars.
How can I check that the string only contains one char and this char is a *
dr["DomainName"].ToString().Contains("*")
checks dr["DomainName"]
contains the char *
, but the condition may also be true if it contains other chars.
How can I check that the string only contains one char and this char is a *
You just have to compare them using String.Equals
or ==
if(dr.Field<string>("DomainName") == "*")
{
// ...
}
String.Contains
is a sub-string search whereas ==
compares the whole string.
All you have to do is to compare the string
to *
:
dr["DomainName"].ToString() == "*";
string.Contains determines whether or not the given string contains the argument, whether that be a single character, or a sub-string that you wish to find.
For example, ("Hello World").Contains("Hello")
as well as ("Hello World").Contains('e')
would both result in true, as the given string (In this case, "Hello World") contains the given argument in both cases. More on string.Contains here.
In your case, however, I understand you're simply trying to determine if the given string is an asterisk, rather than if it contains one. In this case, string.Contains isn't needed! Just do a simple comparison like so: dr["DomainName"].ToString() == "*"
Correct me if I misinterpreted your question in any way, and feel free to comment on this answer if you have any more questions.
Following is the correct condition, to check, if it is equal to *.
dr["DomainName"].ToString() == "*"