-2
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 *

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
John
  • 3,965
  • 21
  • 77
  • 163
  • 6
    erm, `dr["DomainName"].ToString() == "*"` – Jodrell Jul 10 '15 at 09:00
  • If you mean that the string is one or more * & nothing else - then check this : http://stackoverflow.com/questions/16027475/c-sharp-determine-if-all-characters-in-a-string-are-the-same – PaulF Jul 10 '15 at 09:09
  • You're searching for * in the entire string by using 'Contains',so this will turn out to be true when the string contains * even once (atleast) along with other characters.But if you are want to check if the string is just * (only), you should be using the solution posted by jodrell. Anyways, why is this question being downvoted? I'm curious. – RelatedRhymes Jul 10 '15 at 09:33

5 Answers5

5

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

A simple == should do it

dr["DomainName"].ToString() == "*"
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
1

All you have to do is to compare the string to *:

dr["DomainName"].ToString() == "*";
Dzienny
  • 3,295
  • 1
  • 20
  • 30
1

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.

shilovk
  • 11,718
  • 17
  • 75
  • 74
0

Following is the correct condition, to check, if it is equal to *.

dr["DomainName"].ToString() == "*"
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
user3587767
  • 152
  • 8