我有字符串 text="camel",然后我想检查文本是否包含字母“m”,所以我遍历它并使用以下方法检查它:
if (text[i].Equals("m"))
但这永远不会让我回归真实......为什么?
You need to find all occurences of a letter in a text as I understand it:
string text = "camel";
string lookup = "M";
int index = 0;
while ( (index = text.IndexOf(lookup, index, StringComparison.OrdinalIgnoreCase) != -1)
{
// You have found what you looked for at position "index".
}
I don't think that you get it any faster than this.
Good luck with your quest.
Kyle C 已经给了你答案,所以这就是你完成整个过程的方式,我将winforms
以此为例:
private void button1_Click(object sender, EventArgs e)
{
string text = "camel";
if (text.Contains("m") || text.Contains("M"))//also checks for capital M
{
MessageBox.Show("True");
}
}
无奇迹
利用Contains
您是在问“camel”是否等同于“m” - 它不是。
“骆驼”包含“m”。