c# - 如何应用检查只选择简单的字符串而忽略包含日期的字符串?

标签 c#

List<string> mylist = new List<string>()
{
    "element1", "02/18/2019", "element3", "03/17/2018"
};

在上面的列表中,我只想过滤元素 1 和元素 3,而不是包含日期的字符串。我不知道该怎么做。

最佳答案

List<string> mylist = new List<string>()
{
    "element1", "02/18/2019", "element3", "03/17/2018"
}.Where(c => !DateTime.TryParse(c, out DateTime d)).ToList();

编辑:由于格式始终为 MM/dd/yyyy,因此无论线程的语言环境如何,这都应该有效:

List<string> mylist = new List<string>()
{
    "element1", "02/18/2019", "element3", "03/17/2018"
}.Where(c => 
    !DateTime.TryParseExact(c, "MM/dd/yyyy", CultureInfo.InvariantCulture, 
        DateTimeStyles.None, out DateTime d)
    ).ToList();

关于c# - 如何应用检查只选择简单的字符串而忽略包含日期的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60519372/

相关文章:

C# 部署项目 : Icon doesn't appear for the extension associated

c# - MySQL 查询正在执行但在 C# 中未被识别

c# - 从 Win64 位的外部进程文件句柄获取文件名 - C#

c# - 将数组数据从 excel VBA 发送到 WCF

c# - 将文件/资源​​移动到 Visual Studio 中的 bin 文件夹

c# - WindowStartupLocation CenterOwner 如何使用 View 模型?

c# - 在 C# 中解析给定路径

c# - IOS 到 C# 什么等同于 id<>

c# - WCF WebService 中的 GZip 压缩

c# - 如何在不使用 C# 将其设置为默认打印机的情况下直接打印到 zebra 打印机?