VB大虾新春快乐,请问如何编辑txt文件中的数字或文字?
请问对于txt文件中的这行数字,K K K K 23.5 725 ,如何提取23.5的值?相关代码如下:请您将"?"代替的代码写出来,谢谢您Private Sub Command1_Click()
Dim strLine As String
Open "F:\vusefulb\2.txt" For Input As #1
Dim i, x As Integer
i = 0
Do Until EOF(1)
Line Input #1, strLine
?
Close #1
End Sub --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 先用SPLIT 分隔 空格 得到数组
然后判断数组中的值是不是数字 如果是就对了 --------------------编程问答-------------------- 看看能不能满足你的要求.
可将下面代码复制到你的代码中直接运行即可:
Private Sub Command1_Click()
Dim strLine As String
Dim varStringList() As String
Dim varString As Variant
Open "F:\vusefulb\2.txt" For Input As #1
Dim i, x As Integer
Do Until EOF(1)
Line Input #1, strLine
varStringList = Split(strLine, " ")
For Each varString In varStringList
'剔除空字符串
If Trim(varString) <> "" Then
'提取数字部分
If IsNumeric(varString) Then
MsgBox "数字字符串: """ & varString & """"
'只提取23.5
If varString = "23.5" Then
MsgBox "找到23.5: """ & varString & """"
End If
'提取非数字部分
Else
MsgBox "非数字字符串: """ & varString & """"
End If
End If
Next
Loop
Close #1
End Sub --------------------编程问答--------------------
Private Sub Command1_Click()--------------------编程问答-------------------- 用正则表达式吧。
Dim strLine As String
Open "F:\vusefulb\2.txt" For Input As #1
Dim i, x As Integer
i = 0
Do Until EOF(1)
Line Input #1, strLine
a = Split(Trim(strLine), " ")
If a(0) = "K" And a(1) = "K" And a(2) = "K" And a(3) = "K" Then
MsgBox a(4)
End If
Close #1
End Sub
--------------------编程问答--------------------
--------------------编程问答-------------------- dim arr() as string
Private Sub Command1_Click()
Dim strLine As String
Open "F:\vusefulb\2.txt" For Input As #1
Dim i, x As Integer
i = 0
Do Until EOF(1)
Line Input #1, strLine
a = Split(Trim(strLine), " ")
If a(0) = "K" And a(1) = "K" And a(2) = "K" And a(3) = "K" Then
MsgBox a(4)
End If
Loop'少了Loop,添上了
Close #1
End Sub
arr=split(strline," ")
dim i as integer
for i=0 to ubound(arr)
if arr(i)="23.5" then
msgbox "here i found you"
exit for
next
--------------------编程问答-------------------- [Quote=引用楼主 onealjordan2 的回复:]
请问对于txt文件中的这行数字,K K K K 23.5 725 ,如何提取23.5的值?相关代码如下:请您将"?"代替的代码写出来,谢谢您
Private Sub Command1_Click()
Dim strLine As String
Open "F:\vusefulb\2.txt" For Input As #1
Dim i, x As Integer
i = 0
Do Until EOF(1)
Line Input #1, strLine
x=split(strLine)(4)
....
....
loop
Close #1
End Sub
--------------------编程问答-------------------- 如果字符间的空格是一个:
x=split(strLine)(4)
如果字符间的空格是两个:
x=split(strLine, " ")(4) --------------------编程问答-------------------- ?号替换为:
MsgBox RegExpStr("\d+\.\d+\d", strLine)
再增加一个函数
Function RegExpStr(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
For Each Match In Matches
RetStr = Match.Value
Next
RegExpStr = RetStr
End Function
最后别忘了引用正则组件
补充:VB , VBA