C#利用正則表達式實現(xiàn)獲取字符串中漢字的數(shù)量
一、關于字符串和判斷其組成常識
字符串中可以包括數(shù)字、字母、漢字或者其他字符。使用Char類型的IsDigit靜態(tài)方法可以判斷字符串中的某個字符是否為數(shù)字,使用Char類型的IsLetter靜態(tài)方法可以判斷字符串中的某個字符是否為字母。
使用正則表達式判斷字符是否為漢字,進一步了解字符串是一組不可變的字符集的概念,可以使用索引訪問字符串中的每一個字符。
字符串對象的索引是只讀的,只可以讀取字符串對象中的字符,不可以根據(jù)索引更改字符串中的字符。
二、用正則表達式獲取字符串中漢字的數(shù)量
1.字符是否為漢字的正則表達式
Regex regex = MyRegex(); //創(chuàng)建正則表達式對象,用于判斷字符是否為漢字
[GeneratedRegex("^[\u4E00-\u9FA5]{0,}$")]
private static partial Regex MyRegex();
2.實例
// 用正則表達式獲取字符串中漢字的個數(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 = "獲取漢字數(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 = "個"
};
//
// 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 = "獲取漢字數(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; //用于存儲漢字數(shù)量
Regex regex = MyRegex(); //創(chuàng)建正則表達式對象,用于判斷字符是否為漢字
for (int i = 0; i < textBox1!.Text.Length; i++)//遍歷字符串中每一個字符
{
temp = regex.IsMatch(textBox1.Text[i]. //如果字符是漢字則計數(shù)器加1
ToString()) ? ++temp : temp;
}
textBox2!.Text = temp.ToString(); //顯示漢字數(shù)量
}
[GeneratedRegex("^[\u4E00-\u9FA5]{0,}$")]
private static partial Regex MyRegex();
}
}3.生成結果

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

