c:\data.txt中有文本如下:
liuli      21   1303.93  450
当点击Command1时截取这个文本中的数据:1303.93到程序中text1.text中去怎么处理?
注意间隔是空格,不是TAB.请高手以此文本内容举个例,谢谢

解决方案 »

  1.   

    Private Sub Command1_Click()
    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
      

  2.   

    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): Exit SubNext i
      

  3.   

    text2.text="liuli      21   1303.93  450"
      

  4.   

    text2.text="liuli      21   1303.93  450"
    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
      

  5.   

    str1="liuli      21   1303.93  450"Dim s() As string, i As Longv = Split(str1, " ")
    For i = UBound(v) - 2 To 0 Step -1If Trim(v(i)) <> "" Then MsgBox v(i)Next i
      

  6.   

    快成功了,就是得到值只有3个,最后450没显示,我想能否分别写入到text1....text4.text中
      

  7.   

    If Trim(v(i)) <> "" Then Text1.Text = v(i)
    如果这样只能得到最后一个数据。能否有办法分别写入text1-4中?
      

  8.   

    弄错了,一开始你要的是1303.93.把For i = UBound(v) - 2 To 0 Step -1改成For i = UBound(v) - 1 To 0 Step -1就行了.
      

  9.   

    假设 aa = "liuli      21   1303.93  450"
    那么:
    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
      

  10.   

    aa = Trim(Replace(aa, "  ", " "))
    再请教个问题,在其中的"  "是去掉2个空格,后面" "是增加一个空格吗?
      

  11.   

    Dim tmp As String, arr() As Stringtmp = "liuli      21   1303.93  450"Do While Instr(tmp, Space(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
      

  12.   

    off123正解,也可以多个空格替换为某一特定字符,读取后以数组来显示
      

  13.   

    如果是規則的文本文件,
    直接用mid函數取第幾位到第幾位,
    然後用trim函數去掉前後空格即可。
      

  14.   

    Do While Instr(tmp, Space(2))
        tmp = replace(tmp, Space(2), Space(1))  '将多个空格替换为一个空格
    Loop
    如果用多个空格替换成#号,这里应该如何写?
      

  15.   

    Do While Instr(tmp, Space(n))
        tmp = replace(tmp, Space(n), "#")  '将多个空格替换为一个空格
    Loop
    这样就可以了n是空格数量
      

  16.   

    给个通用解决方案你
    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)即可
      

  17.   

    Do While Instr(tmp, Space(2))
        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 将一个空格替换为一个#号了.这里出了什么问题?
      

  18.   

    错了,是2个空格替换成了一个#号
    同理:为何tmp = replace(tmp, Space(2), Space(1))不是将2个空格替换成一个空格?
      

  19.   

    将多个空格替换成一个#号:
    Do While InStr(tmp, " ")
        tmp = Replace(tmp, " ", "#") '将一个空格替换为一个#
    Loop
    Do While InStr(tmp, "##")
        tmp = Replace(tmp, "##", "#") '将多个#替换为一个#
    Loop
    Text1.Text = tmp
    我现在试不了,是这样吧?