使用VB6,如何检查子字符串是否在另一个字符串的开头?

时间:2011-11-21 07:56:43

标签: vb6 pattern-matching

我需要浏览一个文本文件并检查每行的开头是否以“属性”开头。我应该如何在VB6中执行此操作?

4 个答案:

答案 0 :(得分:2)

使用正则表达式。您必须在参考文献中包含VBScript正则表达式库。

Dim reg As new Scripting.Regex().
reg.Pattern = "^Attribute"
If reg.Match(line) Then
     ' Do Something
End If

答案 1 :(得分:1)

Dim sInput As String, check as Boolean
check = true
Open "myfile" For INPUT As #txtFile
While Not EOF(txtFile)
   Input #txtFile, sInput
   If Not Mid(sInput,1,9) = "ATTRIBUTE" Then
       check = false
   End if
   sInput = ""
Wend
Close #txtFile

如果最后check = true,则所有行都以“ATTRIBUTE”开头,否则不会。

答案 2 :(得分:1)

您可以尝试这样的事情(未经过测试的代码) -

Dim ParseDate, AllLinesStartWithAttribute, fso, fs
AllLinesStartWithAttribute = False
Set fso = CreateObject("Scripting.FileSystemObject")
Set fs = fso.OpenTextFile("c:\yourfile", 1, True)
Do Until fs.AtEndOfStream
    If Left(fs.ReadLine, 9) <> "Attribute" Then
       AllLinesStartWithAttribute = False
       Exit Do
    End If
Loop
fs.Close
Set fs = Nothing

如果AllLinesStartWithAttribute值设置为true,则运行代码后,文件中的所有行都以“属性”开头。请注意,此代码区分大小写。

答案 3 :(得分:0)

Dim fso As New FileSystemObject
Dim ts As TextStream
Dim str As String

Set ts = fso.OpenTextFile(MyFile)
Do While Not ts.AtEndOfStream
    str = ts.ReadLine
    If InStr(str, "Attribute") = 1 Then
        ' do stuff
    End If
Loop