如何计算文本文件中的日期数? [英] How to count number of dates in my textfile?
本文介绍了如何计算文本文件中的日期数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何计算文本文件中的日期数?
假设以大写字母写了3-4个日期,那么告诉我C#代码以获取标签控件中这些日期的计数吗?
thanku
How to count number of dates in my textfile?
suppose in full big letter there are 3-4 dates are written so tell me the c# code to get the count of those dates in label control?
thanku
推荐答案
您可以使用正则表达式匹配日期,然后根据需要使用匹配项.
以下链接包含如何在C#中使用RegExp的示例:
C#中的正则表达式用法 [
You can use Regular Expressions to match the dates and then use the matches as you wish.
The following link contains examples how to use RegExp in C#:
Regular Expressions Usage in C#[^]
BTW, the following regexp might help you:
^([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.][0-9]{4}
在以下位置读取文件:
Read the file in:
string[] lines = File.ReadAllLines(path);
处理每一行:
Process each line:
foreach (string line in lines)
{
...
}
如果该行包含日期,请对其进行计数:
If the line contains a date, count it:
int count = 0;
...
foreach (string line in lines)
{
...
count++;
}
将结果放入标签控件:
Put the result to a label control:
myLabel.text = "Number of dates = " + count.ToString();
确定是否包含日期取决于您:我对您的数据格式一无所知...
Working out if it contains a date is up to you: I do not know anything about your data format...
这篇关于如何计算文本文件中的日期数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文