目錄
一、關(guān)于字符串和判斷其組成常識(shí)
二、用正則表達(dá)式獲取字符串中漢字的數(shù)量
1.字符是否為漢字的正則表達(dá)式
2.實(shí)例
3.生成結(jié)果
三、相關(guān)知識(shí)點(diǎn)
1.Regex.IsMatch 方法?
一、關(guān)于字符串和判斷其組成常識(shí)
????????字符串中可以包括數(shù)字、字母、漢字或者其他字符。使用Char類(lèi)型的IsDigit靜態(tài)方法可以判斷字符串中的某個(gè)字符是否為數(shù)字,使用Char類(lèi)型的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)本文作者寫(xiě)的其他文章,C#中使用正則表達(dá)式實(shí)現(xiàn)漢字轉(zhuǎn)拼音-CSDN博客 ?https://wenchm.blog.csdn.net/article/details/135374006文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-798980.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-798980.html
到了這里,關(guān)于C#用正則表達(dá)式獲取字符串中漢字的數(shù)量的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!