Problem With Using Elements Of An String
i have this code for example:
strin开发者_开发知识库g 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?
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");
}
You're comparing a string to an integer.
Try if (i[1] == '0')
.
i[1]
is a char of '0'
(Unicode U+0030), which is different than (int) 0
.
char i[0] is compared against an integer
i[1]
is the second character in the string, as arrays in c# are zero-based.
精彩评论