C#利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量
一、關(guān)于字符串和判斷其組成常識(shí)
字符串中可以包括數(shù)字、字母、漢字或者其他字符。使用Char類型的IsDigit靜態(tài)方法可以判斷字符串中的某個(gè)字符是否為數(shù)字,使用Char類型的IsLetter靜態(tài)方法可以判斷字符串中的某個(gè)字符是否為字母。
使用正則表達(dá)式判斷字符是否為漢字,進(jìn)一步了解字符串是一組不可變的字符集的概念,可以使用索引訪問(wèn)字符串中的每一個(gè)字符。
字符串對(duì)象的索引是只讀的,只可以讀取字符串對(duì)象中的字符,不可以根據(jù)索引更改字符串中的字符。
二、用正則表達(dá)式獲取字符串中漢字的數(shù)量
1.字符是否為漢字的正則表達(dá)式
Regex regex = MyRegex(); //創(chuàng)建正則表達(dá)式對(duì)象,用于判斷字符是否為漢字
[GeneratedRegex("^[\u4E00-\u9FA5]{0,}$")]
private static partial Regex MyRegex();
2.實(shí)例
// 用正則表達(dá)式獲取字符串中漢字的個(gè)數(shù)
using System.Text.RegularExpressions;
namespace _044
{
public partial class Form1 : Form
{
private GroupBox? groupBox1;
private TextBox? textBox2;
private TextBox? textBox1;
private Button? button1;
private Label? label2;
private Label? label1;
public Form1()
{
InitializeComponent();
Load += Form1_Load;
}
private void Form1_Load(object? sender, EventArgs e)
{
//
// textBox2
//
textBox2 = new TextBox
{
Location = new Point(117, 48),
Name = "textBox2",
Size = new Size(133, 23),
TabIndex = 4,
};
//
// textBox1
//
textBox1 = new TextBox
{
Location = new Point(117, 18),
Name = "textBox1",
Size = new Size(162, 23),
TabIndex = 3
};
//
// button1
//
button1 = new Button
{
Location = new Point(17, 48),
Name = "button1",
Size = new Size(97, 23),
TabIndex = 2,
Text = "獲取漢字?jǐn)?shù)量",
UseVisualStyleBackColor = true
};
button1.Click += Button1_Click;
//
// label2
//
label2 = new Label
{
AutoSize = true,
Location = new Point(259, 50),
Name = "label2",
Size = new Size(20, 17),
TabIndex = 1,
Text = "個(gè)"
};
//
// label1
//
label1 = new Label
{
AutoSize = true,
Location = new Point(17, 24),
Name = "label1",
Size = new Size(80, 17),
TabIndex = 0,
Text = "輸入字符串:"
};
//
// groupBox1
//
groupBox1 = new GroupBox
{
Location = new Point(12, 12),
Name = "groupBox1",
Size = new Size(285, 77),
TabIndex = 0,
TabStop = false,
Text = "獲取漢字?jǐn)?shù)量"
};
groupBox1.Controls.Add(textBox2);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(button1);
groupBox1.Controls.Add(label2);
groupBox1.Controls.Add(label1);
groupBox1.SuspendLayout();
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(309, 101);
Controls.Add(groupBox1);
Name = "Form1";
StartPosition = FormStartPosition.CenterScreen;
Text = "獲取字符串中漢字的數(shù)量";
groupBox1.ResumeLayout(false);
groupBox1.PerformLayout();
}
private void Button1_Click(object? sender, EventArgs e)
{
int temp = 0; //用于存儲(chǔ)漢字?jǐn)?shù)量
Regex regex = MyRegex(); //創(chuàng)建正則表達(dá)式對(duì)象,用于判斷字符是否為漢字
for (int i = 0; i < textBox1!.Text.Length; i++)//遍歷字符串中每一個(gè)字符
{
temp = regex.IsMatch(textBox1.Text[i]. //如果字符是漢字則計(jì)數(shù)器加1
ToString()) ? ++temp : temp;
}
textBox2!.Text = temp.ToString(); //顯示漢字?jǐn)?shù)量
}
[GeneratedRegex("^[\u4E00-\u9FA5]{0,}$")]
private static partial Regex MyRegex();
}
}3.生成結(jié)果

三、相關(guān)知識(shí)點(diǎn)
1.Regex.IsMatch 方法
詳見(jiàn)C#中使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音
到此這篇關(guān)于C#利用正則表達(dá)式實(shí)現(xiàn)獲取字符串中漢字的數(shù)量的文章就介紹到這了,更多相關(guān)C#獲取字符串中漢字?jǐn)?shù)量?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# JavaScriptSerializer序列化時(shí)的時(shí)間處理詳解
這篇文章主要為大家詳細(xì)介紹了C# JavaScriptSerializer序列化時(shí)的時(shí)間處理詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
基于動(dòng)態(tài)修改App.Config與web.Config的使用詳解
本篇文章是對(duì)動(dòng)態(tài)修改App.Config與web.Config的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Unity3D Shader實(shí)現(xiàn)鏡子效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)鏡子效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
C#實(shí)現(xiàn)異步連接Sql Server數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)異步連接Sql Server數(shù)據(jù)庫(kù)的方法,涉及C#中await方法的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
c#讀取圖像保存到數(shù)據(jù)庫(kù)中(數(shù)據(jù)庫(kù)保存圖片)
這篇文章主要介紹了使用c#讀取圖像保存到數(shù)據(jù)庫(kù)中的方法,大家參考使用吧2014-01-01
silverlight實(shí)現(xiàn)圖片局部放大效果的方法
這篇文章主要介紹了silverlight實(shí)現(xiàn)圖片局部放大效果的方法,結(jié)合實(shí)例形式分析了silverlight針對(duì)圖片屬性的相關(guān)操作技巧,需要的朋友可以參考下2017-03-03

