String contains only a given set of characters

前端 未结 7 1497
执笔经年
执笔经年 2021-02-18 13:22

I need to know if a given string is a valid DateTime format string because the string may represent other things. I tried DateTime.ParseExact(somedate.ToString(format), format)

7条回答
  •  误落风尘
    2021-02-18 13:45

    Thank you everyone. I 'upped' all of you and settled on a brute force implementation that doesn't use a Dictionary/HashSet and doesn't convert chars to strings:

    private const string DateTimeFormatCharacters = "yYmMdDhHsS";
    private static bool IsDateTimeFormatString(string input)
    {
        foreach (char c in input)
            if (DateTimeFormatCharacters.IndexOf(c) < 0)
                return false;
        return true;
    }
    

提交回复
热议问题