Compare version numbers without using split function(不使用拆分功能比较版本号)
问题描述
如何比较版本号?
例如:
x = 1.23.56.1487.5
x = 1.23.56.1487.5
y = 1.24.55.487.2
y = 1.24.55.487.2
推荐答案
可以使用Version
类吗?
https://docs.microsoft.com/en-us/dotnet/api/system.version
它有一个 IComparable
接口.请注意,这不适用于您展示的 5 部分版本字符串(这真的是您的版本字符串吗?).假设您的输入是字符串,这是一个带有正常 .NET 4 部分版本字符串的工作示例:
It has an IComparable
interface. Be aware this won't work with a 5-part version string like you've shown (is that really your version string?). Assuming your inputs are strings, here's a working sample with the normal .NET 4-part version string:
static class Program
{
static void Main()
{
string v1 = "1.23.56.1487";
string v2 = "1.24.55.487";
var version1 = new Version(v1);
var version2 = new Version(v2);
var result = version1.CompareTo(version2);
if (result > 0)
Console.WriteLine("version1 is greater");
else if (result < 0)
Console.WriteLine("version2 is greater");
else
Console.WriteLine("versions are equal");
return;
}
}
这篇关于不使用拆分功能比较版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不使用拆分功能比较版本号


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01