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

查看:149
本文介绍了如何解析一个月份名称(字符串)为整数在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")

我的谷歌搜索似乎暗示唯一的办法是写自己的方法,但是这似乎是一个常见的​​问题不够,我会想到它会在.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(MONTHNAMEMMMM,CultureInfo.CurrentCulture).Month

虽然,你的目的,你可能会更好只是创建一个词典<字符串,整数方​​式> 映射月份的名字它的值

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天全站免登陆