当前位置:编程学习 > VB >>

VB读非固定格式的TXT

数据格式:A   B C D    E
中间的空格不一样,怎么用VB读

C里面好像有一个函数可以直接读数据,遇到空格就跳过,不知道VB有没有?

--------------------编程问答-------------------- 读出来,再使用Split拆分? --------------------编程问答-------------------- 但是它们中间的空格不一样啊,SPLIT只能处理固定的分隔符 --------------------编程问答--------------------
引用 2 楼 b01041229 的回复:
但是它们中间的空格不一样啊,SPLIT只能处理固定的分隔符

你试了没有?不会啊 --------------------编程问答--------------------

Option Explicit
Dim strP As String

Private Sub Command1_Click()
    Dim intP As Integer
    Dim strA() As String
    strA = Split(strP, " ")
    Text1.Text = ""
    For intP = LBound(strA) To UBound(strA)
        Text1.Text = Text1.Text & strA(intP)
    Next intP
End Sub

Private Sub Form_Load()
    strP = "A B              C   D  E"
End Sub

--------------------编程问答--------------------
引用 4 楼 veron_04 的回复:
VB code

Option Explicit
Dim strP As String

Private Sub Command1_Click()
    Dim intP As Integer
    Dim strA() As String
    strA = Split(strP, " ")
    Text1.Text = ""
    For intP = LBou……

+1 --------------------编程问答--------------------
strp=Replace(strp, " ", "")
--------------------编程问答-------------------- --------------------编程问答--------------------
引用 6 楼 lxq19851204 的回复:
VB code
strp=Replace(strp, " ", "")



你如果把字符串中的空格替掉之后,那拿什么来区分ABCDE呢 --------------------编程问答--------------------
引用 8 楼 b01041229 的回复:
引用 6 楼 lxq19851204 的回复:

VB code
strp=Replace(strp, " ", "")



你如果把字符串中的空格替掉之后,那拿什么来区分ABCDE呢


Private Sub Form_Load()

Dim a, b
Dim i As Integer

    a = "A   B   CD  E"
    b = Replace(a, " ", "")
    For i = 1 To Len(b)
        a = Mid(b, i, 1)
    Next i
    
End Sub
--------------------编程问答--------------------
while instr(strp,"  ")>0
    strp=Replace(strp, "  ", " ")
wend
--------------------编程问答-------------------- 去掉空格再读如何? --------------------编程问答--------------------
引用 4 楼 veron_04 的回复:
VB code

Option Explicit
Dim strP As String

Private Sub Command1_Click()
    Dim intP As Integer
    Dim strA() As String
    strA = Split(strP, " ")
    Text1.Text = ""
    For intP = LBou……

Option Explicit
Dim strP As String

Private Sub Command1_Click()
    Dim intP As Integer
    Dim strA() As String
    dim s As String
    strA = Split(strP, " ")
    For intP = LBound(strA) To UBound(strA)
        if strA(intP)<>"" then s=s+"["+strA(intP)+"]"
    Next intP
    msgbox s
End Sub

Private Sub Form_Load()
    strP = "A B              C   D  E"
End Sub


--------------------编程问答-------------------- 先用replace删除掉多于的空格,只保留一个空格作为间隔符,然后再使用Split拆分。
比如:

Sub main()
    Dim i As Integer
    Dim s As String
    Dim arr() As String
    
    s = "a   b  c             d"
    
    '过滤掉多余的空格
    Do
        s = Replace(s, "  ", " ")
    Loop Until InStr(s, "  ") = 0

    '拆分并显示结果
    arr = Split(s, " ")
    For i = 0 To UBound(arr)
        Debug.Print i, arr(i)
    Next
End Sub
--------------------编程问答--------------------


'*****读取文件表头信息********************************************
Sub ReadFileInfo()
    Open MngBaseFileName For Input As #intFilenum
         Dim a() As String
         Line Input #intFilenum, strline
         Deltblank   '转到 Deltblank 去除多余空格 
         a = Split(strline)
         v01000 = a(0)
         v04001 = a(6)
         v04002 = a(7)
    Close #intFilenum
End Sub
'*****去除多余的空格********************************************
Sub Deltblank()
   Do
      If InStr(1, strline, "  ", vbTextCompare) = 0 Then Exit Do
      strline = Replace(strline, "  ", " ")
   Loop
End Sub


这是我常用的方法
很管用,如果格式里面还有TAB格式的话可以先替换成空格


'*****去除多余的空格********************************************
Sub Deltblank()
   strline = Replace(Trim(strline), Chr(9), " ")
   Do
      If InStr(1, strline, "  ", vbTextCompare) = 0 Then Exit Do
      strline = Replace(strline, "  ", " ")
   Loop
End Sub
--------------------编程问答-------------------- 路过,学习学习…… --------------------编程问答-------------------- 用正则将多个空格替换成一个,然后分割;或者只取非空数据。

Sub GetStr()
   Dim oJs As Object,Str$
   Str = "A  B C   D     E       F G"

   Set oJS = CreateObject("ScriptControl"):oJS.Language = "JScript"
   Ojs.eval "function gets{return str.match(/\S+/g,'')}"

   Str = oJs.Codeobject.gets(Str)
   Debug.Print Str
End Sub

--------------------编程问答-------------------- 使用正则表达式不就可以了 --------------------编程问答--------------------

Private Sub Command1_Click()
    Dim N As String, i As Integer
    Dim R() As Integer
    
    N = "123 33  55   66  77"
    While InStr(N, " ")
          ReDim Preserve R(i)
          R(i) = Mid(N, 1, InStr(N, " ") - 1)
          N = Trim(Mid(N, InStr(N, " ")))
          i = i + 1
    Wend
    ReDim Preserve R(i)
    R(i) = N
    
End Sub

补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,