如何将月份名称(字符串)解析为整数以在 C# 中进行比较? [英] How to parse a month name (string) to an integer for comparison in C#?

查看:16
本文介绍了如何将月份名称(字符串)解析为整数以在 C# 中进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够比较数组中的一些月份名称.

I need to be able to compare some month names I have in an array.

如果有一些直接的方式就好了:

It would be nice if there were some direct way like:

Month.toInt("January") > Month.toInt("May")

我的 Google 搜索似乎表明唯一的方法是编写自己的方法,但这似乎是一个足够普遍的问题,我认为它已经在 .Net 中实现了,以前有人这样做过吗?

My Google searching seems to suggest the only way is to write your own method, but this seems like a common enough problem that I would think it would have been already implemented in .Net, anyone done this before?

推荐答案

DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture ).Month

不过,就您的目的而言,您最好只创建一个 Dictionary 将月份的名称映射到其值.

Although, for your purposes, you'll probably be better off just creating a Dictionary<string, int> mapping the month's name to its value.

这篇关于如何将月份名称(字符串)解析为整数以在 C# 中进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆