當前位置:編程學習大全網 - 編程軟體 - C#中如何搜索壹個string中壹***出現某個字符的次數?

C#中如何搜索壹個string中壹***出現某個字符的次數?

這個需要靠編程,可以通過循環,每次搜索到壹個red就加1.

給妳參考代碼:

private void richTextBox1_TextChanged(object sender, EventArgs e)

{

label1.Text = string.Format("string count:{0}", GetCount(richTextBox1.Text));

}

private int GetCount(string str)

{

int cunt=0;

string[] strs = str.Split(new char[] { ' ' });

foreach(string tpStr in strs)

{

if(tpStr.ToLower().IndexOfAny(

new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',

'r','s','t','u','v','w','x','y','z'})>=0)

{

cunt++;

}

}

return cunt;

}

  • 上一篇:馬鞍山學院是幾年級?
  • 下一篇:c#和c++區別
  • copyright 2024編程學習大全網