'目的是把IP地址的后三位,转换为数字形式:“44135122”,
'以下代码执行结果却为:301,即只有后三位IP数字直接相加
Dim mc As String
Dim ii As Integer
mc = "172.44.135.122"
Dim Values() As String
Values = mc.split(".")
For Index As Integer = 1 To Values.Length -1
Select Case Index
Case Index =1
ii = ii + Values (Index) * 1000000
Case Index = 2
ii = ii +Values (Index) * 1000
Case Else
ii = ii +Values (Index)*1
End Select
Next
Output.Show(ii)
Dim mc As String = "172.44.135.122"
Dim i As Integer = mc.indexof(".")
mc = mc.substring(i + 1).Replace(".", "")
Dim ii As Integer = CInt(mc)
Output.Show(ii)
直接删除后三位“.”还不行。
如遇到IP数字段只用二位数字时,则顺序变化不符合习惯(大小顺序)
修改为以下代码,输出符合要求。
Dim mc, a, b, c As String
Dim ii As Integer
mc = "172.44.12.12"
Dim Values() As String
Values = mc.split(".")
For Index As Integer = 1 To Values.Length - 1
Select Case Index
Case 1
a = Values (Index) & "000000"
Case 2
b = Values (Index) & "000"
Case Else
c = Values (Index)
End Select
Next
ii = Val(a) + Val(b) + Val(c)
Output.Show(ii)
[此贴子已经被作者于2024/7/24 7:19:14编辑过]
Dim mc As String = "172.44.12.12"
Dim Values() As String
Values = mc.split(".")
mc = ""
For i As Integer = 1 To Values.Length - 1
mc = mc & Values(i).PadLeft(3, "0")
Next
Dim ii As Integer = CInt(mc)
Output.Show(ii)
Dim 数组() As String = "172.44.12.12".Split(".")
Output.Show(数组(1) & String.Join("", 数组.skip(2).select(Function(元素) 元素.PadLeft(3, "0"))))
不管是一位还是二位,均能补全
[此贴子已经被作者于2024/7/24 14:47:25编辑过]