问题描述
大家好,
有点奇怪的问题我不得不做一个例程来读入并更新序列号,
以字符串形式读入,以字符串形式发送给相关电路板。但是,添加1以增加它时会出现问题。如果我使用1234567890并使用Val将其转换为数字它可以工作,我可以添加1并将其保存回1234567891.但问题是,如果它是0000000000.这个Val转换为0(因为它全是0的)如果我添加一个,它是1,没有前导0。我问了很多关于C#的类似问题,得到了一个正则表达式的答案。我正在尝试做的快速演示如下:
Hi All,
Bit of a strange question I am having to do a routine to read in and update a serial number,
Read in as a string, send to board in question as a string. However the problem comes when adding 1 to increment it. If I use 1234567890 and use a Val to convert it to a number it works and I can add 1 to it and save it back out 1234567891. However the problem comes if it is 0000000000. This Val translates to 0 (because it is all 0's) if I add one to it, it is 1 with no leading 0's. I asked a similar question in relation to C# many moons ago and got a regex answer. A quick demo of what I am trying to do is below:
Dim line As String = System.IO.File.ReadAllText("SerialNumber.ini.txt")
TextBox1.Text = line
Label1.Text = line
SearchWithinThis = line
MsgBox(SearchWithinThis)
FirstCharacter = SearchWithinThis.IndexOf(SearchForThis)
MsgBox(FirstCharacter)
LineModded = line.Substring(FirstCharacter + 1, 10)
LineModded += 1
MsgBox(LineModded)
我只是害怕会出现什么问题!
格伦
I am just afraid of what can go wrong!
Glenn
推荐答案
Dim s1 As String = "000000000000"
Dim l As Long = Val(s1)
Debug.Print(l.ToString())
Dim s2 As String = (l + 1).ToString().PadLeft(11, "0")
Debug.Print(s2)
给出输出
gives output
0
00000000001
LineModded = (CInt(line) + 1).ToString("0000000000")
这篇关于在VB中添加前导0的正确程序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!