且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算C#WPF中文本文件中的特殊字符

更新时间:2023-02-20 10:52:13

%^& *()_ + - )在文本框中。我想计算文本框中只出现特殊字符和输出。



样本输出:

## 242 >
@的数量:212





.etc



我尝试过:



protected void OFD_Click(object sender,RoutedEventArgs e)

{

OpenFileDialog openFile = new OpenFileDialog();



if(openFile.ShowDialog()== true)

{

string filepath = openFile.FileName;

textbox.Text = File.ReadAllText(filepath);





}}
%^&*()_+-) in textbox. I want to count the occurrence of only special characters and output in textbox.

sample output:
Number of #: 242
Number of @ : 212
.
.
.etc

What I have tried:

protected void OFD_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();

if (openFile.ShowDialog()==true)
{
string filepath = openFile.FileName;
textbox.Text = File.ReadAllText(filepath);


}}


见这里:计算字符串中的行 [ ^ ] - 它与行有关,但它们只是一个特殊分隔符(即'\ n')你可以用一系列可搜索的字符替换它们。

它提供了多种方法,并告诉你每种方法的可能性能成本。
See here: Counting Lines in a String[^] - it's to do with lines, but they are just a "special delimiter" (i.e. '\n') which you could replace with an array of "searchable characters".
It presents a variety of ways to do it, and tells you the probable performance cost of each.


所以为了做到这一点,我建议你读一读在正则表达式,但这是一个例子



正则表达式类(System.Text.RegularExpressions) [ ^ ]



正则表达式语言 - 快速参考| Microsoft Docs [ ^ ]



C#正则表达式 [ ^ ]



[ ^ ]



So in order to do this I suggest you read up on regexes but here is an example

Regex Class (System.Text.RegularExpressions)[^]

Regular Expression Language - Quick Reference | Microsoft Docs[^]

C# Regular Expressions[^]

[^]

// Define special characters in regex string
var regexString = @"[(~!@#