|
在对字符串进行比较时,可以先调用ToUpper方法或ToLower方法将字符串全部转换为大写或小写,然后再调用string类的Compare方法进行比较。Compare方法有3个返回值,如表1所示。 表1 Compare方法返回值及说明 返回值 | 说明 | 小于0 | strA小于strB | 0 | strA等于strB | 大于0 | strA大于strB |
例如,下面的代码用来在比较textBox1文本框和textBox2文本框中的内容时,忽略大小写: if (string.Compare(textBox1.Text.ToLower(), textBox2.Text.ToLower()) < 0) MessageBox.Show("字符串1小于字符串2", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); if (string.Compare(textBox1.Text.ToLower(), textBox2.Text.ToLower()) > 0) MessageBox.Show("字符串1大于字符串2", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); if (string.Compare(textBox1.Text.ToLower(), textBox2.Text.ToLower()) == 0) MessageBox.Show("字符串1等于字符串2", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); |
|