Reputation: 3071
i have this code for example:
string i = "100";
if(i[1]==0)
{
MessageBox.Show("ok");
}
and I thought I should get "ok" but it doesnt work. What is i[1]
here?
Upvotes: 0
Views: 61
Reputation: 136239
i[1]
is the second character in the string, as arrays in c# are zero-based.
Upvotes: 0
Reputation: 245499
Your comparison is using the wrong type. When you use an indexer with a string, the result is a char
. Your if statement is using an int
. You need to change your code to:
if(i[1] == '0')
{
MessageBox.Show("Ok");
}
Upvotes: 6
Reputation: 100047
i[1]
is a char of '0'
(Unicode U+0030), which is different than (int) 0
.
Upvotes: 2