100012,fwafawf,防瓦房
100013,aasfgew,规定法国
.........
现有类似这样的文本 N 行. 数字 , 字母+符号 , 中文或英文或加符号没准
目的.
Dim iNum(1 To 10000) As Long
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
iNum(1) = 100009
yNum(1) = fwajflawkj
wNum(1) = 左右
iNum(2) = 100012
yNum(2) = fwafawf
wNum(2) = 防瓦房
iNum(3) = ........
各种搜索只搜到 Split 方法.只能换下标 我想先换变量名.然后再换下标
我这个不适合啊? 或者是我不会用.
求思路或代码片断...
求个老师...OTZ... 自学.心好累啊...
16 个解决方案
#1
其实区别不大的
'---------------------------------------------------------------------------------------
' 过程名 : SplitMultiLines
' 时间 : 2010年3月10日
' 作者 : 杨过.网狐.cn
' 功能 : http://topic.csdn.net/u/20100310/17/433bd4fb-3044-463d-9dab-a61d27a4dbef.html?66932
' 说明 : 分割字符串问题.在等 - -
'---------------------------------------------------------------------------------------
'
Public Sub SplitMultiLines()
Dim i As Long
Dim strText As String, strLine As String
Dim strLineArr() As String, strSubArr() As String
Dim iNum(1 To 10000) As Long
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
Dim iNumNo As Long
iNumNo = 0
strText = "100009,fwajflawkj,左右" & vbCrLf & _
"100012,fwafawf,防瓦房" & vbCrLf & _
"100013,aasfgew,规定法国"
strLineArr = Split(strText, vbCrLf)
For i = 0 To UBound(strLineArr)
strLine = strLineArr(i)
If (Len(strLineArr(i)) > 0) Then
strSubArr = Split(strLine, ",")
If (UBound(strSubArr) = 2) Then
iNumNo = iNumNo + 1
iNum(iNumNo) = Val(strSubArr(0))
yNum(iNumNo) = strSubArr(1)
wNum(iNumNo) = strSubArr(2)
Else
MsgBox "数据格式不正确"
End If
End If
'插入记录到数据库
Next i
End Sub
#2
Split(VbCrLf)先按照行分割。
然后Split(",")分割,存入临时数组。
再循环装入你要的数组。
然后Split(",")分割,存入临时数组。
再循环装入你要的数组。
#3
DTYaty32split 记录,再redim 记录集,最后填字段
moke520
..理解不能.. - -
初学. 能再说详细点吗?
DTYaty32记录间是 回车分割的
split 得记录条数,redim 分配存储空间
字段间是 逗号分割的
moke520
redim 分配存储空间
- - redim 没用过啊 - -
只能怪自己. 想这样的解释我是理解不了的 ....
moke520
..理解不能.. - -
初学. 能再说详细点吗?
DTYaty32记录间是 回车分割的
split 得记录条数,redim 分配存储空间
字段间是 逗号分割的
moke520
redim 分配存储空间
- - redim 没用过啊 - -
只能怪自己. 想这样的解释我是理解不了的 ....
#4
dim ss1() as String,ss2() as String
'取得N行文本为tmp
tmp="100009,fwajflawkj,左右" & vbcrlf & "100012,fwafawf,防瓦房" & "100013,aasfgew,规定法国" & vbcrlf
ss1=split(tmp,vbcrlf)
for i=0 to Ubound(ss1)
ss2=split(ss1,vbcrlf)
iNum(i) = ss2(0)
yNum(i) = ss2(1)
wNum(i) = ss2(2)
next
'取得N行文本为tmp
tmp="100009,fwajflawkj,左右" & vbcrlf & "100012,fwafawf,防瓦房" & "100013,aasfgew,规定法国" & vbcrlf
ss1=split(tmp,vbcrlf)
for i=0 to Ubound(ss1)
ss2=split(ss1,vbcrlf)
iNum(i) = ss2(0)
yNum(i) = ss2(1)
wNum(i) = ss2(2)
next
#5
Private AllStr As String
Private Sub Form_Load()
Dim iStr() As String
dim i as Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
iNum(1) = iStr(0)
yNum(1) = iStr(1)
wNum(1) = iStr(2)
' Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
我只能写成这样了 - - 但是这样写的话 以后想取值就取不到了
dim i as Integer
是为以后用到某个字符串准备的..
这样写的话 .. 最终目的达不到 - -
Private Sub Form_Load()
Dim iStr() As String
dim i as Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
iNum(1) = iStr(0)
yNum(1) = iStr(1)
wNum(1) = iStr(2)
' Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
我只能写成这样了 - - 但是这样写的话 以后想取值就取不到了
dim i as Integer
是为以后用到某个字符串准备的..
这样写的话 .. 最终目的达不到 - -
#6
更正 ss2=split(ss1,vbcrlf) 为 ss2=split(ss1,",")
呵呵,已经有人回了
呵呵,已经有人回了
#7
谢谢2楼和4楼的老师.....
但是我那个是从文本读进去的..
不止那三行.....
大概 - - 10000行....
但是我那个是从文本读进去的..
不止那三行.....
大概 - - 10000行....
#8
open "1.txt" for input as #1
str = input(1, lof(1))
close #1
#9
修改一下
Private AllStr As String
Private Sub Form_Load()
Dim iStr() As String
Dim i As Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
iNum(i) = iStr(0)
yNum(i) = iStr(1)
wNum(i) = iStr(2)
i = i + 1
'Debug.Print iNum(i) & yNum(i) & wNum(i)
Loop
Close #1
End Sub
#10
修改一下,没看你的数组从1开始的。呵呵
Private AllStr As String
Private Sub Form_Load()
Dim iStr() As String
Dim i As Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
i = i + 1
iNum(i) = iStr(0)
yNum(i) = iStr(1)
wNum(i) = iStr(2)
'Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
End Sub
#11
对于你行数比较多,用get语句来处理应该比较快些
后面再用Split( ,VbCrLf),split( ,",")处理
Open App.Path & "c:\1.txt" For Binary As #1
ReDim buffer(LOF(fNO))
Get #1, 1, buffer
strTmp = Trim(StrConv(buffer(), vbUnicode))
close #1
后面再用Split( ,VbCrLf),split( ,",")处理
#12
参考2楼的....
strText = "100009
...........
我把我的显示后面
Text1.Text = Text1.Text & iNum(1) & yNum(1) & wNum(1) & vbCrLf
加了一句
AllStr = Text1.Text
这样好像按他那个办法就行了..
我快结贴吧. 再有人来帮忙的话我都感觉我拿的这点分对不起大家了~
谢谢...谢谢楼上的几个老师. 我都会好好看的..
strText = "100009
...........
我把我的显示后面
Text1.Text = Text1.Text & iNum(1) & yNum(1) & wNum(1) & vbCrLf
加了一句
AllStr = Text1.Text
这样好像按他那个办法就行了..
我快结贴吧. 再有人来帮忙的话我都感觉我拿的这点分对不起大家了~
谢谢...谢谢楼上的几个老师. 我都会好好看的..
#13
谢谢.
skylinecn
他写的那个是我能完全理解的. 呵呵..
其他人的还要再看看..
skylinecn
他写的那个是我能完全理解的. 呵呵..
其他人的还要再看看..
#14
Private AllStr As String
Private Sub Form_Load()
Dim iStr() As String
Dim i As Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
i = i + 1
iNum(i) = iStr(0)
yNum(i) = iStr(1)
wNum(i) = iStr(2)
'Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
End Sub
Private Sub Form_Load()
Dim iStr() As String
Dim i As Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
i = i + 1
iNum(i) = iStr(0)
yNum(i) = iStr(1)
wNum(i) = iStr(2)
'Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
End Sub
#15
虽然结贴了,本着讨论的精神,还是发一下我的看法:
如果你文本行数太多的话,建议一次性读取,我照你的路径修改了一下代码,你参考一下。(欢迎讨论)
如果你文本行数太多的话,建议一次性读取,我照你的路径修改了一下代码,你参考一下。(欢迎讨论)
Private Sub Form_Load()
Dim s$(), n&, i&, ss$()
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Binary As #1
s = Split(Trim(Input(LOF(1), #1)), vbCrLf)
Close #1
ReDim iNum$(0), yNum$(0), wNum$(0)
For i = 0 To UBound(s)
If Trim(s(i)) <> "" Then
ss = Split(s(i), ",")
ReDim Preserve iNum(n), yNum(n), wNum(n)
iNum(n) = ss(0): yNum(n) = ss(1): wNum(n) = ss(2)
n = n + 1
End If
Next i
End Sub
#16
再次感谢. skylinecn
#1
其实区别不大的
'---------------------------------------------------------------------------------------
' 过程名 : SplitMultiLines
' 时间 : 2010年3月10日
' 作者 : 杨过.网狐.cn
' 功能 : http://topic.csdn.net/u/20100310/17/433bd4fb-3044-463d-9dab-a61d27a4dbef.html?66932
' 说明 : 分割字符串问题.在等 - -
'---------------------------------------------------------------------------------------
'
Public Sub SplitMultiLines()
Dim i As Long
Dim strText As String, strLine As String
Dim strLineArr() As String, strSubArr() As String
Dim iNum(1 To 10000) As Long
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
Dim iNumNo As Long
iNumNo = 0
strText = "100009,fwajflawkj,左右" & vbCrLf & _
"100012,fwafawf,防瓦房" & vbCrLf & _
"100013,aasfgew,规定法国"
strLineArr = Split(strText, vbCrLf)
For i = 0 To UBound(strLineArr)
strLine = strLineArr(i)
If (Len(strLineArr(i)) > 0) Then
strSubArr = Split(strLine, ",")
If (UBound(strSubArr) = 2) Then
iNumNo = iNumNo + 1
iNum(iNumNo) = Val(strSubArr(0))
yNum(iNumNo) = strSubArr(1)
wNum(iNumNo) = strSubArr(2)
Else
MsgBox "数据格式不正确"
End If
End If
'插入记录到数据库
Next i
End Sub
#2
Split(VbCrLf)先按照行分割。
然后Split(",")分割,存入临时数组。
再循环装入你要的数组。
然后Split(",")分割,存入临时数组。
再循环装入你要的数组。
#3
DTYaty32split 记录,再redim 记录集,最后填字段
moke520
..理解不能.. - -
初学. 能再说详细点吗?
DTYaty32记录间是 回车分割的
split 得记录条数,redim 分配存储空间
字段间是 逗号分割的
moke520
redim 分配存储空间
- - redim 没用过啊 - -
只能怪自己. 想这样的解释我是理解不了的 ....
moke520
..理解不能.. - -
初学. 能再说详细点吗?
DTYaty32记录间是 回车分割的
split 得记录条数,redim 分配存储空间
字段间是 逗号分割的
moke520
redim 分配存储空间
- - redim 没用过啊 - -
只能怪自己. 想这样的解释我是理解不了的 ....
#4
dim ss1() as String,ss2() as String
'取得N行文本为tmp
tmp="100009,fwajflawkj,左右" & vbcrlf & "100012,fwafawf,防瓦房" & "100013,aasfgew,规定法国" & vbcrlf
ss1=split(tmp,vbcrlf)
for i=0 to Ubound(ss1)
ss2=split(ss1,vbcrlf)
iNum(i) = ss2(0)
yNum(i) = ss2(1)
wNum(i) = ss2(2)
next
'取得N行文本为tmp
tmp="100009,fwajflawkj,左右" & vbcrlf & "100012,fwafawf,防瓦房" & "100013,aasfgew,规定法国" & vbcrlf
ss1=split(tmp,vbcrlf)
for i=0 to Ubound(ss1)
ss2=split(ss1,vbcrlf)
iNum(i) = ss2(0)
yNum(i) = ss2(1)
wNum(i) = ss2(2)
next
#5
Private AllStr As String
Private Sub Form_Load()
Dim iStr() As String
dim i as Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
iNum(1) = iStr(0)
yNum(1) = iStr(1)
wNum(1) = iStr(2)
' Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
我只能写成这样了 - - 但是这样写的话 以后想取值就取不到了
dim i as Integer
是为以后用到某个字符串准备的..
这样写的话 .. 最终目的达不到 - -
Private Sub Form_Load()
Dim iStr() As String
dim i as Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
iNum(1) = iStr(0)
yNum(1) = iStr(1)
wNum(1) = iStr(2)
' Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
我只能写成这样了 - - 但是这样写的话 以后想取值就取不到了
dim i as Integer
是为以后用到某个字符串准备的..
这样写的话 .. 最终目的达不到 - -
#6
更正 ss2=split(ss1,vbcrlf) 为 ss2=split(ss1,",")
呵呵,已经有人回了
呵呵,已经有人回了
#7
谢谢2楼和4楼的老师.....
但是我那个是从文本读进去的..
不止那三行.....
大概 - - 10000行....
但是我那个是从文本读进去的..
不止那三行.....
大概 - - 10000行....
#8
open "1.txt" for input as #1
str = input(1, lof(1))
close #1
#9
修改一下
Private AllStr As String
Private Sub Form_Load()
Dim iStr() As String
Dim i As Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
iNum(i) = iStr(0)
yNum(i) = iStr(1)
wNum(i) = iStr(2)
i = i + 1
'Debug.Print iNum(i) & yNum(i) & wNum(i)
Loop
Close #1
End Sub
#10
修改一下,没看你的数组从1开始的。呵呵
Private AllStr As String
Private Sub Form_Load()
Dim iStr() As String
Dim i As Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
i = i + 1
iNum(i) = iStr(0)
yNum(i) = iStr(1)
wNum(i) = iStr(2)
'Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
End Sub
#11
对于你行数比较多,用get语句来处理应该比较快些
后面再用Split( ,VbCrLf),split( ,",")处理
Open App.Path & "c:\1.txt" For Binary As #1
ReDim buffer(LOF(fNO))
Get #1, 1, buffer
strTmp = Trim(StrConv(buffer(), vbUnicode))
close #1
后面再用Split( ,VbCrLf),split( ,",")处理
#12
参考2楼的....
strText = "100009
...........
我把我的显示后面
Text1.Text = Text1.Text & iNum(1) & yNum(1) & wNum(1) & vbCrLf
加了一句
AllStr = Text1.Text
这样好像按他那个办法就行了..
我快结贴吧. 再有人来帮忙的话我都感觉我拿的这点分对不起大家了~
谢谢...谢谢楼上的几个老师. 我都会好好看的..
strText = "100009
...........
我把我的显示后面
Text1.Text = Text1.Text & iNum(1) & yNum(1) & wNum(1) & vbCrLf
加了一句
AllStr = Text1.Text
这样好像按他那个办法就行了..
我快结贴吧. 再有人来帮忙的话我都感觉我拿的这点分对不起大家了~
谢谢...谢谢楼上的几个老师. 我都会好好看的..
#13
谢谢.
skylinecn
他写的那个是我能完全理解的. 呵呵..
其他人的还要再看看..
skylinecn
他写的那个是我能完全理解的. 呵呵..
其他人的还要再看看..
#14
Private AllStr As String
Private Sub Form_Load()
Dim iStr() As String
Dim i As Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
i = i + 1
iNum(i) = iStr(0)
yNum(i) = iStr(1)
wNum(i) = iStr(2)
'Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
End Sub
Private Sub Form_Load()
Dim iStr() As String
Dim i As Integer
Dim iNum(1 To 10000) As String
Dim yNum(1 To 10000) As String
Dim wNum(1 To 10000) As String
i = 0
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, AllStr
iStr() = Split(AllStr, ",")
i = i + 1
iNum(i) = iStr(0)
yNum(i) = iStr(1)
wNum(i) = iStr(2)
'Debug.Print iNum(1) & yNum(1) & wNum(1)
Loop
Close #1
End Sub
#15
虽然结贴了,本着讨论的精神,还是发一下我的看法:
如果你文本行数太多的话,建议一次性读取,我照你的路径修改了一下代码,你参考一下。(欢迎讨论)
如果你文本行数太多的话,建议一次性读取,我照你的路径修改了一下代码,你参考一下。(欢迎讨论)
Private Sub Form_Load()
Dim s$(), n&, i&, ss$()
Open "C:\Documents and Settings\Xia\桌面\1.txt" For Binary As #1
s = Split(Trim(Input(LOF(1), #1)), vbCrLf)
Close #1
ReDim iNum$(0), yNum$(0), wNum$(0)
For i = 0 To UBound(s)
If Trim(s(i)) <> "" Then
ss = Split(s(i), ",")
ReDim Preserve iNum(n), yNum(n), wNum(n)
iNum(n) = ss(0): yNum(n) = ss(1): wNum(n) = ss(2)
n = n + 1
End If
Next i
End Sub
#16
再次感谢. skylinecn