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;
}
}
这篇关于不使用拆分功能比较版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不使用拆分功能比较版本号
基础教程推荐
- 将数据集转换为列表 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 如果条件可以为空 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
