try
{
string str = File .ReadAllText ( @"map40x10.txt" );
// int hz = Regex .Matches ( str , @"[\u4E00-\u9FFF]" ) .Count;//汉字
// int en = Regex .Matches ( str , "[A-Za-z]" ) .Count;
int num = Regex .Matches ( str , @"\d" ) .Count;//数字
int nr = Regex .Matches ( str , @"\r" ) .Count + 1;//行数
Console .WriteLine ( "数字个数={0},行数={1}" , num , nr );
Console .WriteLine ( str );
}
————————————————
版权声明:本文为优快云博主「_lessismore」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/zjc_game_coder/article/details/64921398
c#获得txt文件中字符的个数
最新推荐文章于 2022-09-29 21:47:06 发布
本文介绍了一种使用C#读取文本文件的方法,并通过正则表达式统计文件中的数字数量和行数。该代码示例展示了如何使用File.ReadAllText方法读取文件内容,以及如何使用Regex.Matches进行模式匹配。
622

被折叠的 条评论
为什么被折叠?



