C# 如何比较版本?
C# how can you compare versions?
如何比较两个版本号?
我不想要简单的 1.0.0.0,但我正在寻找比较这些
1.0.0.1
1.0.0.1rc1 / 1.0.0.1-rc1
1.0.0.1b
1.0.0.1a / 1.0.0.1-a
等等
它们是按照最新的方向排列的。
引用 PHP.net 的函数,满足我的需求
version_compare() compares two "PHP-standardized" version number strings.
The function first replaces _, - and + with a dot . in the version strings and also inserts dots . before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. Then it compares the parts starting from left to right. If a part contains special version strings these are handled in the following order: any string not found in this list < dev < alpha = a < beta = b < RC = rc < # < pl = p. This way not only versions with different levels like '4.1' and '4.1.2' can be compared but also any PHP specific version containing development state.
如何做到这一点?
或者默认有这样的东西吗?
使用semantic version library for .Net
安装包:
Install-Package semver
您可以解析/比较版本。
示例 1:比较
var v1 = new SemVersion(1, 0, 0, "rc1");
Console.WriteLine(v1);
var v2 = new SemVersion(1, 0, 0, "rc2");
Console.WriteLine(v2);
var r = SemVersion.Compare(v1,v2);
//If v1 < v2 return -1
//if v1 > v2 return +1
//if v1 = v2 return 0
Console.WriteLine(r); // -1
示例 2:解析
var version = SemVersion.Parse("1.2.45-alpha-beta+nightly.23.43-bla");
Console.WriteLine(version);
Console.WriteLine( version.Major); //1
Console.WriteLine( version.Minor); //2
Console.WriteLine( version.Patch); //45
Console.WriteLine(version.Prerelease); //alpha-beta
Console.WriteLine(version.Build); //nightly.23.43
更新:
生活演示在fiddle
另一种方式是使用yadyn的SemanticVersion
您可以点击here 复制代码。
您还可以从 git tool 复制代码,该代码有超过 100 个贡献者。
与其使用第 3 方的库,不如仅引用 Nuget.Core 并利用它们的 SemanticVersion class。 class 的工作方式与 .net 中的标准 Version 对象非常相似,但遵循语义版本控制规范 (https://semver.org)。它会将您的字符串解析为 IComparable 和 IEquatable 对象,以便您可以比较多个版本或在集合中对它们进行排序等。
Nuget.Core: https://www.nuget.org/packages/nuget.core/(你可以通过 nuget 拉取这个库)
https://github.com/NuGet/NuGet2/blob/2.13/src/Core/SemanticVersion.cs
var rawVersions = new [] {"v1.4.0", "v1.4.0-patch10", "v1.4.0-patch2"};
var versions = rawVersions.Select(v => new SemanticVersion(v));
var sorted = versions.ToList().Sort();
我比较两个版本的功能。版本示例“1.22.333.4444”
static int CompareVersions(string First, string Second)
{
var IntVersions = new List<int[]>
{
Array.ConvertAll(First.Split('.'), int.Parse),
Array.ConvertAll(Second.Split('.'), int.Parse)
};
var Cmp = IntVersions.First().Length.CompareTo(IntVersions.Last().Length);
if (Cmp == 0)
IntVersions = IntVersions.Select(v => { Array.Resize(ref v, IntVersions.Min(x => x.Length)); return v; }).ToList();
var StrVersions = IntVersions.ConvertAll(v => string.Join("", Array.ConvertAll(v,
i => { return i.ToString($"D{IntVersions.Max(x => x.Max().ToString().Length)}"); })));
var CmpVersions = StrVersions.OrderByDescending(i => i).ToList();
return CmpVersions.First().Equals(CmpVersions.Last()) ? Cmp : CmpVersions.First().Equals(StrVersions.First()) ? 1 : -1;
}
First > Second
1
First == Second
0
First < Second
-1
如何比较两个版本号?
我不想要简单的 1.0.0.0,但我正在寻找比较这些
1.0.0.1
1.0.0.1rc1 / 1.0.0.1-rc1
1.0.0.1b
1.0.0.1a / 1.0.0.1-a
等等
它们是按照最新的方向排列的。
引用 PHP.net 的函数,满足我的需求
version_compare() compares two "PHP-standardized" version number strings. The function first replaces _, - and + with a dot . in the version strings and also inserts dots . before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'. Then it compares the parts starting from left to right. If a part contains special version strings these are handled in the following order: any string not found in this list < dev < alpha = a < beta = b < RC = rc < # < pl = p. This way not only versions with different levels like '4.1' and '4.1.2' can be compared but also any PHP specific version containing development state.
如何做到这一点?
或者默认有这样的东西吗?
使用semantic version library for .Net
安装包:
Install-Package semver
您可以解析/比较版本。
示例 1:比较
var v1 = new SemVersion(1, 0, 0, "rc1");
Console.WriteLine(v1);
var v2 = new SemVersion(1, 0, 0, "rc2");
Console.WriteLine(v2);
var r = SemVersion.Compare(v1,v2);
//If v1 < v2 return -1
//if v1 > v2 return +1
//if v1 = v2 return 0
Console.WriteLine(r); // -1
示例 2:解析
var version = SemVersion.Parse("1.2.45-alpha-beta+nightly.23.43-bla");
Console.WriteLine(version);
Console.WriteLine( version.Major); //1
Console.WriteLine( version.Minor); //2
Console.WriteLine( version.Patch); //45
Console.WriteLine(version.Prerelease); //alpha-beta
Console.WriteLine(version.Build); //nightly.23.43
更新:
生活演示在fiddle
另一种方式是使用yadyn的SemanticVersion
您可以点击here 复制代码。
您还可以从 git tool 复制代码,该代码有超过 100 个贡献者。
与其使用第 3 方的库,不如仅引用 Nuget.Core 并利用它们的 SemanticVersion class。 class 的工作方式与 .net 中的标准 Version 对象非常相似,但遵循语义版本控制规范 (https://semver.org)。它会将您的字符串解析为 IComparable 和 IEquatable 对象,以便您可以比较多个版本或在集合中对它们进行排序等。
Nuget.Core: https://www.nuget.org/packages/nuget.core/(你可以通过 nuget 拉取这个库)
https://github.com/NuGet/NuGet2/blob/2.13/src/Core/SemanticVersion.cs
var rawVersions = new [] {"v1.4.0", "v1.4.0-patch10", "v1.4.0-patch2"};
var versions = rawVersions.Select(v => new SemanticVersion(v));
var sorted = versions.ToList().Sort();
我比较两个版本的功能。版本示例“1.22.333.4444”
static int CompareVersions(string First, string Second)
{
var IntVersions = new List<int[]>
{
Array.ConvertAll(First.Split('.'), int.Parse),
Array.ConvertAll(Second.Split('.'), int.Parse)
};
var Cmp = IntVersions.First().Length.CompareTo(IntVersions.Last().Length);
if (Cmp == 0)
IntVersions = IntVersions.Select(v => { Array.Resize(ref v, IntVersions.Min(x => x.Length)); return v; }).ToList();
var StrVersions = IntVersions.ConvertAll(v => string.Join("", Array.ConvertAll(v,
i => { return i.ToString($"D{IntVersions.Max(x => x.Max().ToString().Length)}"); })));
var CmpVersions = StrVersions.OrderByDescending(i => i).ToList();
return CmpVersions.First().Equals(CmpVersions.Last()) ? Cmp : CmpVersions.First().Equals(StrVersions.First()) ? 1 : -1;
}
First > Second | 1 |
First == Second | 0 |
First < Second | -1 |