日期:2014-05-17  浏览次数:21140 次

*文本文件读取,并查询其中指定的字符输出到屏幕*
有个文件文件在   mappath( "1.txt ")

里面内容如下:

kj.3   kj.7   aj.1   aj,2
kj.1   kj.4   aj,d2   aj.3d

我想只读取其中的   kj.开头的字符串按读出顺序和从小到大顺序分别输出到屏幕,如:
kj.3
kj.7
kj.1
kj.4
---
kj.1
kj.3
kj.4
kj.7

------解决方案--------------------
上面的比较没考虑到字符比较容易出错的问题,而且根据某一个特征修改代码也不方便,所以重贴完整代码:

dim dArr()
dim icount,i,j
dim fso,f
dim dataValue,data
dim preValue

icount=0
preValue= "kj. "

'读取文件,并且用空格 " "分割成数组
set fso=server.createobject( "scripting.filesystemobject ")
set f=fso.openTextFile(server.mappath( "1.txt "))
if not f.atendofstream then
data=f.readAll
end if
f.close
set f=nothing
set fso=nothing
data=replace(data,vbcrlf, " ")
dataArr=split(data, " ")

for i=0 to ubound(dataArr) '求出后面的数字,并且存入数组dArr中
if dataArr(i) <> " " and not isNull(dataArr(i)) then
if lcase(left(dataArr(i),len(preValue)))=preValue then
redim preserve dArr(icount)
dArr(icount)=right(dataArr(i),len(dataArr(i))-len(preValue))
icount=icount+1
end if
end if
next

for i=0 to ubound(dArr)
Response.Write preValue&dArr(i)& " <br> "
next
response.write "------- "& " <br> "
'起泡排序
if icount=0 then
Response.Write "没有任何数据 "
Response.End
end if

for i=0 to ubound(dArr)
for j=i+1 to ubound(dArr)
if int(dArr(i))> =int(dArr(j)) then
dataValue=dArr(i)
dArr(i)=dArr(j)
dArr(j)=dataValue
end if
next
response.write preValue&dArr(i)& " <br> "
next