例如:在c:\1.txt文本中有如下内容(文本内容不定长的):
abcq***enqe**&ewsx*(&
*3asdf34&312&&***需求1:现在需要将文本中是"*"和"&"的字符进行保留,其他字符全部转换为空格,即得到:
    ***    **&    * &
*       &   &&***
需求2:将文本中的"*"和"&"的个数进行统计,如上即为15个
请教高人指点一下.

解决方案 »

  1.   

    凑合着能完成任务
    Dim fso As New Scripting.filesystemobject
    Dim ts As Scripting.textstream
    Dim ret As String
    Dim backup As String
    Dim pos1 As Integer, pos2 As IntegerSet ts = fso.opentextfile("c:\1.txt")
    Do While ts.AtEndOfStream <> True
        pos = 1
        ret = ts.readline
        Do While True
        pos1 = InStr(pos, ret, "&")
        pos2 = InStr(pos, ret, "*")
        If pos1 = 0 And pos2 = 0 Then
            GoTo continue
        End If
        If pos1 = 0 Then pos = pos2 + 1
        If pos2 = 0 Then pos = pos1 + 1
        If (pos1 = pos Or pos2 = pos) Then
            backup = backup + Mid(ret, pos, 1)
        Else
            backup = backup + " " + Mid(ret, pos, 1)
        End If
        pos = IIf(pos1 < pos2, pos1, pos2) + 1
        Loop
        Trim (backup)
        Debug.Print backup
    continue:
    Loop
    ts.Close
      

  2.   

    Dim fso As New Scripting.filesystemobject
    Dim ts As Scripting.textstream这是定义的什么类型??提示“用户定义类型未定义”
      

  3.   

    Dim s As String
        s = "abcq***enqe**&ewsx*(&*3asdf34&312&&***"
        Dim reg As Object
        Set reg = CreateObject("vbscript.regexp")
        reg.Pattern = "[!&\*]"
        reg.IgnoreCase = True
        reg.MultiLine = True
        reg.Global = True
        MsgBox reg.Replace(s, " ")
        reg.Pattern = "[&\*]"
        MsgBox reg.Execute(s).Count