This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




8年前关闭。




我正在尝试拆分我当前的应用程序版本号,但删除了前导零。

如何更改我的拆分以不删除前导零。

获取当前版本号:
startUpAssembly.GetName().Version.ToString()

所以为了测试:
string versionNo = "7.01.7000.0";

string[] versionInfo = versionNo.Split('.');

这产生:
7
1 //Here i need it to be 01
7000
0

我需要它不删除前导零。我如何实现这一目标?

也许使用正则表达式有更好的解决方案?

最佳答案

System.Version 不是任意字符串 - 它是四个整数。前导零无关紧要,因此在转换回字符串时不包括在内。这就是您丢失信息的地方 - 而不是 String.Split 。你可以很容易地看到这一点:

using System;

class Test
{
    static void Main()
    {
        Version version = new Version("7.01.7000.0");
        Console.WriteLine(version); // 7.1.7000.0
    }
}

基本上,你的计划从根本上是有缺陷的,你应该改变你的设计。你不应该试图代表“7.01.7000.0”的一个版本开始。

此外,您应该退后一步思考您的诊断程序:是什么让您认为 String.Split 应该归咎于此?为什么您的第一步不是查看 startUpAssembly.GetName().Version.ToString() 的结果?

关于c# - String.Split() 删除前导零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13470678/

10-13 07:39