【发布时间】:2013-07-14 13:55:02
【问题描述】:
这应该是不言自明的。我正在尝试检测字符串 foo 的第一个字符是否为负号“-”。这只是一个测试代码来测试它。
private void button1_Click(object sender, EventArgs e)
{
string foo = textBox1.Text;
bool negativeValue = foo[1]==('-');
//bool negativeValue = foo[1].Equals ('-');
if (negativeValue == true)
{
label1.Text = "First char is negative !";
}
else if (negativeValue == false)
{
label1.Text = "First char is not negative !";
}
}
即使文本框中的第一个字符是“-”,结果也始终为 false。为什么?
【问题讨论】:
-
在这种情况下,您可以通过调试应用程序并查看
foo[1]的值很容易发现错误。