C#字數統計(字母、數字、漢字、符號)

來源:互聯網
上載者:User

標籤:ips   git   span   form   pre   component   class   turn   int   

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();//字數                        textBox6.Text = cnt2.ToString();//位元組數量(不含" ","\t","\n","\r"," ?")                        textBox7.Text = cnt3.ToString();//數字數量                        textBox8.Text = cnt4.ToString();//字母數量                        textBox9.Text = (cnt2 - cnt1).ToString();//漢字數量                        textBox10.Text = (cnt2 - cnt3 - cnt4 - (cnt2 - cnt1) * 2).ToString();//符號數量                    }                }            }        }        //richTextBox1.text內容改變事件        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();//字數                    textBox6.Text = cnt2.ToString();//位元組數量(不含" ","\t","\n","\r"," ?")                    textBox7.Text = cnt3.ToString();//數字數量                    textBox8.Text = cnt4.ToString();//字母數量                    textBox9.Text = (cnt2 - cnt1).ToString();//漢字數量                    textBox10.Text = (cnt2 - cnt3 - cnt4 - (cnt2 - cnt1)*2).ToString();//符號數量                }            }        }         /// <summary>        /// 返回字數        /// </summary>        /// <param name="s"></param>        /// <returns></returns>        private int getWordLength(string s)        {            if (s != null)                return s.Length;            else                return 0;        }        /// <summary>        /// 返回數字(0~9)字數數量        /// </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)字數數量        /// </summary>        /// <param name="s"></param>        /// <returns></returns>        private int getcharLength(string s)        {            int lz = 0;            char[] q = s.ToLower().ToCharArray();//大寫字母轉換成小寫字母            for (int i = 0; i < q.Length; i++)            {                if ((int)q[i] >= 97 && (int)q[i] <= 122)//小寫字母                {                    lz += 1;                }            }            return lz;        }        /// <summary>        /// 返回位元組數        /// </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;        }    }}

 摘自http://blog.csdn.net/ou832339/article/details/20343439

C#字數統計(字母、數字、漢字、符號)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.