Is it better to use ToCharArray () or ToString () when comparing string and char?

I need to check if a string matches a specific char.

At the moment I am doing it like this:

if (InputData.ToCharArray()[0] == 0x18)



is a string, and whenever I get to that point in my code, it should always only be char length.

My question is, would it be preferable to compare in some other way? For example, for example:

if (InputData == ((char)0x1c).ToString())



source to share

2 answers

You can do it like this:

string InputData = "...";
if (InputData.Length == 1 && InputData[0] == 0x18)




There are many ways to do this, for example:

string InputData = "...";
if(InputData.Length == 1 && InputData[0].Equals(0x18))




All Articles