c:\data.txt中有文本如下:
liuli 21 1303.93 450
当点击Command1时截取这个文本中的数据:1303.93到程序中text1.text中去怎么处理?
注意间隔是空格,不是TAB.请高手以此文本内容举个例,谢谢
liuli 21 1303.93 450
当点击Command1时截取这个文本中的数据:1303.93到程序中text1.text中去怎么处理?
注意间隔是空格,不是TAB.请高手以此文本内容举个例,谢谢
解决方案 »
- 想请教下,迅雷的网页FLASH探测,在FLASH上面显示的按钮是怎么实现的呢
- vb能不能操作flash cookies---编辑、删除、列表?
- 各位仁兄:怎样从oracle 中读取字段,用来执行判断?
- 【散分】工作一年后的苦闷……
- 如何把excel中的数据导入到SQL 2000数据库中???
- 关于vb定时的问题:
- 请问,如何用SQL实现向一个已有的表的第一个和第二个字段中插入一个字段,用ALTER ADD 只能添加在最后呀
- 请问,我想实现表格输入的功能,还可以保存,打印,导入,用什么控件好?
- 如何把400多M的文件写到资源里
- 怎样捕捉动态生成的按钮的事件
- 怎么将word文档转成pdf文件
- 非高手勿看,如何使用VB编程操纵网页?
Dim s1 As String
Open "C:\data.txt" For Input As #1 '打开文件
Input #1, s1 '读取文本
Text1.Text = s1 '显示在text文本中
Close
End Sub
这样显示为全部份数据,我的目的是以空格为标识来分段取值,截取文本内容如下:
text1.text = liuli
text2.text = 21
text3.text = 1303.93
text4.text = 450
For i = UBound(v) - 2 To 0 Step -1If Trim(v(i)) <> "" Then MsgBox v(i): Exit SubNext i
Dim v As Variant, i As Longv = Split(Text2.Text, " ")
For i = UBound(v) - 2 To 0 Step -1If Trim(v(i)) <> "" Then MsgBox v(i)Next i
For i = UBound(v) - 2 To 0 Step -1If Trim(v(i)) <> "" Then MsgBox v(i)Next i
如果这样只能得到最后一个数据。能否有办法分别写入text1-4中?
那么:
aa = trim(replace(aa," "," ")) '去掉多余的空格
dim bb() as string
bb = split(aa," ")
if ubound(bb) >= 3 then
Text1.Text = bb(0) 'liuli
Text2.Text = bb(1) '21
Text3.Text = bb(2) '1303.93
Text4.Text = bb(3) '450
else
msgbox "格式不对"
end if
再请教个问题,在其中的" "是去掉2个空格,后面" "是增加一个空格吗?
tmp = replace(tmp, Space(2), Space(1)) '将多个空格替换为一个空格
Loop
tmp = Trim(tmp)arr = Split(tmp, Space(1))
If Ubound(arr) = 3 Then
Text1 = arr(0) 'liuli
Text2 = arr(1) '21
Text3 = arr(2) '1303.93
Text4 = arr(3) '450
Else
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
MsgBox "格式错误!", vbCritical, "错误"
End If
直接用mid函數取第幾位到第幾位,
然後用trim函數去掉前後空格即可。
tmp = replace(tmp, Space(2), Space(1)) '将多个空格替换为一个空格
Loop
如果用多个空格替换成#号,这里应该如何写?
tmp = replace(tmp, Space(n), "#") '将多个空格替换为一个空格
Loop
这样就可以了n是空格数量
Public Function Word(ByVal sSource As String, _
n As Long) As String
'=================================================
' Word retrieves the nth word from sSource
' Usage:
' Word("red blue green ", 2) "blue"
'=================================================
Const SP As String = " "
Dim pointer As Long 'start parameter of Instr()
Dim Pos As Long 'position of target in InStr()
Dim X As Long 'word count
Dim lEnd As Long 'position of trailing word delimitersSource = CSpace(sSource)'find the nth word
X = 1
pointer = 1Do
Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces
pointer = pointer + 1
Loop
If X = n Then 'the target word-number
lEnd = InStr(pointer, sSource, SP) 'pos of space at end of word
If lEnd = 0 Then lEnd = Len(sSource) + 1 ' or if its the last word
Word = Mid$(sSource, pointer, lEnd - pointer)
Exit Do 'word found, done
End If
Pos = InStr(pointer, sSource, SP) 'find next space
If Pos = 0 Then Exit Do 'word not found
X = X + 1 'increment word counter
pointer = Pos + 1 'start of next word
Loop
End FunctionPublic Function Words(ByVal sSource As String) As Long
'=================================================
' Words returns the number of words in a string
' Usage:
' Words("red blue green") 3
'=================================================
Const SP As String = " "
Dim lSource As Long 'length of sSource
Dim pointer As Long 'start parameter of Instr()
Dim Pos As Long 'position of target in InStr()
Dim X As Long 'word countsSource = CSpace(sSource)
lSource = Len(sSource)
If lSource = 0 Then Exit Function'count words
X = 1
pointer = 1Do
Do While Mid$(sSource, pointer, 1) = SP 'skip consecutive spaces
pointer = pointer + 1
Loop
Pos = InStr(pointer, sSource, SP) 'find next space
If Pos = 0 Then Exit Do 'no more words
X = X + 1 'increment word counter
pointer = Pos + 1 'start of next word
Loop
If Mid$(sSource, lSource, 1) = SP Then X = X - 1 'adjust if trailing space
Words = X
End Function调用
Word("liuli 21 1303.93 450",3)即可
tmp = replace(tmp, Space(2), Space(1)) '将多个空格替换为一个空格
Loop
text1.text = tmp
运行结果为:liuli 21 1303.93 450 是正常的。但是如果将多个空格替换为#符号时
Do While Instr(tmp, Space(2))
tmp = replace(tmp, Space(2), "#") '将多个空格替换为一个#
Loop
text1.text = tmp
运行结果为:liuli###21# 1303.93#450 将一个空格替换为一个#号了.这里出了什么问题?
同理:为何tmp = replace(tmp, Space(2), Space(1))不是将2个空格替换成一个空格?
Do While InStr(tmp, " ")
tmp = Replace(tmp, " ", "#") '将一个空格替换为一个#
Loop
Do While InStr(tmp, "##")
tmp = Replace(tmp, "##", "#") '将多个#替换为一个#
Loop
Text1.Text = tmp
我现在试不了,是这样吧?