用數(shù)組唄。我是用VB6的,不過你會.NET也肯定能看懂。
成都創(chuàng)新互聯(lián)公司專注于金昌網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供金昌營銷型網(wǎng)站建設(shè),金昌網(wǎng)站制作、金昌網(wǎng)頁設(shè)計、金昌網(wǎng)站官網(wǎng)定制、小程序設(shè)計服務(wù),打造金昌網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供金昌網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
不是文本文件么?先用Line Input讀每行存入數(shù)組。再把每行數(shù)據(jù)用你的","分割,就可以查詢了。我寫個簡單的例子:
'搜索函數(shù),用法Search(標(biāo)頭,序號),返回數(shù)據(jù).
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 '行數(shù)
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個數(shù)據(jù),就用Search("gc",5)即可返回45。
不能用空格作分隔符,這樣一定會出錯。
可以用逗號分開的,Split不能作為拆分以空格作為分隔符的字符串。
sj = MSComm1.Input
A() = Split(sj, ",")
現(xiàn)在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
上面的代碼是負(fù)責(zé)把“a b c”用空格分開的字符串賦值給a數(shù)組,然后輸出a的所有元素值的,看起來沒有任何錯誤。但是肯定出現(xiàn)下標(biāo)越界的錯誤,因為不能用空格作為分隔符,一定要記住!
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??"正數(shù)的個數(shù)為?"??n
str?=?str??"最大元素的下標(biāo)為?"
For?i?=?1?To?UBound(a)
If?a(i)?=?temp?Then
str?=?str??i??"?"
End?If
Next
TextBox6.Text?=?str