C#統(tǒng)計(jì)字符串的方法
本文實(shí)例為大家分享了C#字?jǐn)?shù)統(tǒng)計(jì)(字母、數(shù)字、漢字、符號)的具體代碼,供大家參考,具體內(nèi)容如下
namespace 測試1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.richTextBox1.Text = "g你\rs\rs你好嗎d dsDDDDDDDDddssssss、‘\\奇巧';‘l;''''sssssssssssssssssssssssssssssssssssssssssssssssssssssssssasdddddddddddddddddddd\tddddddddddddddddddddddddddddddddddddddddddddddddda";
}
bool skipSpace = true;
//窗口加載
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Focus();
int cnt1=0, cnt2=0, cnt3=0, cnt4=0;
if (this.richTextBox1.Text != null)
{
string sLine;
string s = richTextBox1.Text;
if (skipSpace)
{
{
sLine = s.Replace(" ?", "").Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
cnt2 += getByteLength(sLine);
cnt1 += getWordLength(sLine);
cnt3 += getdigitalLength(sLine);
cnt4 += getcharLength(sLine);
textBox5.Text = cnt1.ToString();//字?jǐn)?shù)
textBox6.Text = cnt2.ToString();//字節(jié)數(shù)量(不含" ","\t","\n","\r"," ?")
textBox7.Text = cnt3.ToString();//數(shù)字?jǐn)?shù)量
textBox8.Text = cnt4.ToString();//字母數(shù)量
textBox9.Text = (cnt2 - cnt1).ToString();//漢字?jǐn)?shù)量
textBox10.Text = (cnt2 - cnt3 - cnt4 - (cnt2 - cnt1) * 2).ToString();//符號數(shù)量
}
}
}
}
//richTextBox1.text內(nèi)容改變事件
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
bool skipSpace = true;
int cnt1=0, cnt2=0, cnt3=0, cnt4=0;
if (this.richTextBox1.Text != null)
{
string sLine;
string s = richTextBox1.Text;
if (skipSpace)
{
sLine = s.Replace(" ?", "").Replace(" ", "").Replace("\t", "").Replace("\n", "").Replace("\r", "");
cnt2 += getByteLength(sLine);
cnt1 += getWordLength(sLine);
cnt3 += getdigitalLength(sLine);
cnt4 += getcharLength(sLine);
textBox5.Text = cnt1.ToString();//字?jǐn)?shù)
textBox6.Text = cnt2.ToString();//字節(jié)數(shù)量(不含" ","\t","\n","\r"," ?")
textBox7.Text = cnt3.ToString();//數(shù)字?jǐn)?shù)量
textBox8.Text = cnt4.ToString();//字母數(shù)量
textBox9.Text = (cnt2 - cnt1).ToString();//漢字?jǐn)?shù)量
textBox10.Text = (cnt2 - cnt3 - cnt4 - (cnt2 - cnt1)*2).ToString();//符號數(shù)量
}
}
}
/// <summary>
/// 返回字?jǐn)?shù)
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private int getWordLength(string s)
{
if (s != null)
return s.Length;
else
return 0;
}
/// <summary>
/// 返回?cái)?shù)字(0~9)字?jǐn)?shù)數(shù)量
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private int getdigitalLength(string s)
{
int lx = 0;
char[] q = s.ToCharArray();
for (int i = 0; i < q.Length; i++)
{
if ((int)q[i] >= 48 && (int)q[i] <= 57)
{
lx += 1;
}
}
return lx;
}
/// <summary>
/// 返回字母(A~Z-a~z)字?jǐn)?shù)數(shù)量
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private int getcharLength(string s)
{
int lz = 0;
char[] q = s.ToLower().ToCharArray();//大寫字母轉(zhuǎn)換成小寫字母
for (int i = 0; i < q.Length; i++)
{
if ((int)q[i] >= 97 && (int)q[i] <= 122)//小寫字母
{
lz += 1;
}
}
return lz;
}
/// <summary>
/// 返回字節(jié)數(shù)
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private int getByteLength(string s)
{
int lh = 0;
char[] q = s.ToCharArray();
for (int i = 0; i < q.Length; i++)
{
if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5) // 漢字
{
lh += 2;
}
else
{
lh += 1;
}
}
return lh;
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫建模的基本教程
這篇文章主要介紹了C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫建模的基本教程,LINQ to SQL被集成在.NET框架之中,需要的朋友可以參考下2016-03-03
C#實(shí)現(xiàn)的4種常用數(shù)據(jù)校驗(yàn)方法小結(jié)(CRC校驗(yàn),LRC校驗(yàn),BCC校驗(yàn),累加和校驗(yàn))
本文主要介紹了C#實(shí)現(xiàn)的4種常用數(shù)據(jù)校驗(yàn)方法小結(jié)(CRC校驗(yàn),LRC校驗(yàn),BCC校驗(yàn),累加和校驗(yàn)),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
C#中const,readonly和static關(guān)鍵字的用法介紹
這篇文章介紹了C#中const,readonly和static關(guān)鍵字的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
C# 9.0新特性——擴(kuò)展方法GetEnumerator支持foreach循環(huán)
這篇文章主要介紹了C# 9.0新特性——擴(kuò)展方法GetEnumerator支持foreach循環(huán)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c# 9.0,感興趣的朋友可以了解下2020-11-11
詳解C# parallel中并行計(jì)算的四種寫法總結(jié)
在C#中,parallel關(guān)鍵字可以用于并行計(jì)算。本文為大家總結(jié)了四種C# parallel并行計(jì)算的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11
C# 9 中新加入的關(guān)鍵詞 init,record,with
這篇文章主要介紹了C# 9 中新加入的關(guān)鍵詞 init,record,with的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)c# 9,感興趣的朋友可以了解下2020-08-08

