用数组呗。我是用VB6的,不过你会.NET也肯定能看懂。
成都创新互联公司专注于金昌网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供金昌营销型网站建设,金昌网站制作、金昌网页设计、金昌网站官网定制、小程序设计服务,打造金昌网络公司原创品牌,更为您提供金昌网站排名全网营销落地服务。
不是文本文件么?先用Line Input读每行存入数组。再把每行数据用你的","分割,就可以查询了。我写个简单的例子:
'搜索函数,用法Search(标头,序号),返回数据.
Private Function Search(ByVal Section As String, ByVal Index As Integer) As String
Dim fNum%, Lines%, temp%, Str As String
ReDim Data(0)
fNum = FreeFile()
If Dir("C:\1.txt") = "" Then Exit Function '文件路径和文件名你自己改
Open "C:\1.txt" For Input As #fNum
Do While Not EOF(fNum)
Lines = Lines + 1 '行数
Line Input #fNum, Str
ReDim Preserve Data(Lines)
Data(Lines) = Str
Loop
Close #fNum
If Lines 0 Then
Dim tmp() As String
For temp = 1 To UBound(Data)
tmp = Split(Data(temp), ",") '分割
If tmp(0) = Section Then
Search = tmp(Index - 1) '因为从0开始所以-1
Exit Function
End If
Next
End If
End Function
比如你要“gc“开头的第5个数据,就用Search("gc",5)即可返回45。
不能用空格作分隔符,这样一定会出错。
可以用逗号分开的,Split不能作为拆分以空格作为分隔符的字符串。
sj = MSComm1.Input
A() = Split(sj, ",")
现在A(0)是+3.28742752E+01,A(1)是+0.000,A(2)是+00000,A(3)是101,A(4)是+3.32506905E+01,A(4)是+0.044,A(5)是+00001,A(6)是102(如果加上后面的1,A(6)是102 1)
举一个空格分开的例子:
Dim a() As String
a = Split("a b c", " ")
For i = 1 To UBound(a)
Print a(i)
Next i
上面的代码是负责把“a b c”用空格分开的字符串赋值给a数组,然后输出a的所有元素值的,看起来没有任何错误。但是肯定出现下标越界的错误,因为不能用空格作为分隔符,一定要记住!
Dim str As String = "", temp, n As Integer
Dim str1 As String = "12,45,2,9,41,31,66,83,2,1,-9,-91,-21"
Dim a() As String = Split(str1, ",")
For i = 1 To UBound(a) Step 1
a(i) = Val(a(i))
Next
temp = 0
n = 0
For i = 1 To UBound(a)
If a(i) temp Then
temp = a(i)
End If
If a(i) 0 Then
n = n + 1
End If
Next
str = str "正数的个数为 " n
str = str "最大元素的下标为 "
For i = 1 To UBound(a)
If a(i) = temp Then
str = str i " "
End If
Next
TextBox6.Text = str