用C#获取一个国家的洲
有没有办法在 C# 中使用 RegionInfo
类或任何其他方式查找国家/地区的大陆?
例如,您给出国家/地区名称“丹麦”,它会返回“欧洲”。
Is there a way to find a country's continent in C#, using the RegionInfo
class or any other way?
For example, you give the country name "Denmark" and it returns "Europe".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
.NET 框架中没有类似的情况。它对欧亚大陆和大洋洲的地理定义也相当模糊。目前使用的有 6 种不同的定义。您需要自己的数据库。
Nothing like that in the .NET framework. It is also rather ambiguous with geographical definitions for Eurasia and Oceania. There are 6 distinct definitions in use. You'll need your own dbase.
您可以存储哈希表。这些条目将以国家/地区名称作为键,以大陆名称作为值。
只需从一些公共数据库生成此表,一旦获得它,您就可以简单地查询它。它也相当快。
http://www.geonames.org/countries/
您可以从那里获取数据库
You can store a hash table. The entries will have as key the country name and as value the continent name.
Just generate this table from some public database and once you have it you can simply query it. It's also pretty fast.
http://www.geonames.org/countries/
You can get from there a database
只有几大洲和几百个国家,因此构建自己的桌子并不困难。对于大陆到底是什么(澳大利亚/大洋洲/澳大利亚/亚洲有时是一个特别棘手的大陆),以及土耳其和俄罗斯是否属于欧洲、亚洲或两者都存在意见分歧。尽管如此,自己动手至少可以让你在这些问题上做出自己的决定。
There are only a few continents and only a couple of hundred countries, so it wouldn't be very hard just to build your own table. There are differences of opinion as to just what the continents are (Australia/Oceania/Australasia/Asia being a particularly prickly one at times), and whether e.g. Turkey and Russia are in Europe, Asia, or both. Still, rolling your own at least lets you make your own call on these matters.
您可以使用
http://products.wolframalpha.com/api/
http://www.wolframalpha.com/input/?i=大陆+THECOUNTRY
http://www.wolframalpha.com/input/?i=大陆+瑞典
You can use
http://products.wolframalpha.com/api/
http://www.wolframalpha.com/input/?i=continent+THECOUNTRY
http://www.wolframalpha.com/input/?i=continent+sweden
我知道这是一个老问题,但我已经为 C# 创建了一个库,其中包含内容信息。您可以在这里找到我的项目 Nager.Country 我希望它对您有所帮助。
nuget 包也可用
install-package Nager.Country
示例
I know it's an old question but i have create a library for C# and this contains the contintent information. You can found my project here Nager.Country I hope it helps you.
A nuget package is also available
install-package Nager.Country
Example
RegionInfo 不存储大陆。但您可以使用 GeoNames.org 国家/地区信息 构建数据库。数据格式为 utf8 编码的制表符分隔文本。它包含您需要的所有信息以及更多信息。
RegionInfo doesn't store continent. But you can build a database using GeoNames.org Country Information. The data format is tab-delimited text in utf8 encoding. It's has all the information and more that you need.
您可以根据您需要的大陆模型构建自己的查找列表。
例如,在美国和英国,我们将美国视为两个独立的大陆(北美和南美),而大多数其他国家的人们都被告知美国是一个大陆。
您还应该考虑到一些国家位于两大洲。俄罗斯的一部分在欧洲,其余部分在亚洲。土耳其也一样。
You can build your own lookup lists according to the continent model you need.
For example, in the US and the UK we consider America as two separate continents (North America and South America), whereas people in most other countries are taught that America is a single continent.
You should also consider that some countries lay in two continents. A part of Russia is in Europe, the rest is in Asia. Same with Turkey.