如何修改我的代码以使用多个数字 [英] How to modify my code to use more than one digit numbers
问题描述
这是我用来从文本文件中读取某些值的代码.该程序会在文本中查找特定的字符串,当找到该字符串时,它将在"="符号后将字符串拆分,并在等号后返回该值.
在这里;
This is the code i''m using to read certain values from a text file. The program looks for a particular string in a text and when it is found, then it splits the string after "=" sign and return the value after equals sign.
Here;
Dim myvalue As Integer
For Each k As String In IO.File.ReadLines("d:\test.txt")
If k.Contains("stringXYZ=") Then
Dim values() As String = k.Split(CChar("=")).ToArray
myvalue = Convert.ToInt32(values(1))
End If
Next
Select case myvalue
case 1
MsgBox("Returns 1")
case 2
MsgBox("Returns 2")
End select
但是它无法返回任何两位或更多数字的值.
另外,在极少数情况下,我需要能够读取类似"stringxyz = 1500.000"的值.有什么办法可以做到这一点?我想不通...
VB 2010和.net 4.0
But it fails to return any values with two or more digits.
Plus in some rare cases, i need to be able to read values like this "stringxyz=1500.000". Is there any way to make this possible? I couldn''t figure it out...
VB 2010 and .net 4.0
推荐答案
拆分输入时,请勿将其转换为char数组.而是使用返回的字符串.就像这样:
When you split the input, dont convert it to char array. Instead use the returned strings. So something like:
...
Dim values() As String = k.Split(CChar("="))
myvalue = Convert.ToInt32(values(1))
...
另外,关于计算:
一种蛮力方式是根据整数部分的长度来划分原始数字.例如,请考虑以下内容:
Addition, about the calculation:
One brute-force way is to divide the original number based on the integer part length. For example consider following:
Dim a1 As String
Dim a2 As Double
Dim a3 As Double
Dim a4 As Double
a1 = "15000.000"
a2 = Double.Parse(a1)
a3 = Math.Pow(10, (a1.IndexOf(".") - 2))
a4 = a2 / a3
实际的解决方案应该首先检查数字是否太小,以此类推
The actual solution should check that the number isn''t too small in the first place and so on
这篇关于如何修改我的代码以使用多个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!