从txt文件中选择某些字符串,VB6

时间:2015-01-15 19:26:16

标签: string vb6

我试图找出如何从VB6中打开的txt文件中选择某些字符串。 我已经设法打开文件,并将其分配给变量(不确定是否可行)。已打开的txt文件还包含其他不相关的信息,因此需要使用相关信息创建新文件。 我写了一些算法,虽然它不是很好:P

'READ Line 01
'REPEAT
   'IF line begins with "studentname=" then
      'Copy to new textbox, in new textbox/variable create new line
   'ELSE If line begins with "studentID="
      'Copy to new textbox, in new textbox/variable create 2 new lines
   'ENDIF
   'READ next line
'UNTIL end of text is reached

有谁知道我怎么能做到这一点? 感谢

1 个答案:

答案 0 :(得分:3)

这是您的问题的示例代码:

Filename$="myfilename.txt"       'This row assigns to variable Filename$
                                 'the name of your file, example: 
                                 '"myfilename.txt"

Open Filename$ For Input As #1   'This row opens the file Filename$ 

  Do While Not EOF(1)            'This row inits the loop to read all 
                                 'lines in your file and loop ends when
                                 'there is not others lines to read

   Line Input #1, line$          'This row inserts on variable line$ a
                                 'line of your file

   If mid(line$,1,12)="studentname=" then 'In this row the command mid 
                                          ' extracts the first 12 chars 
                                          'of line and verify if is 
                                          'equal to "studentname="

    Text1.Text=mid(line$,13)     'Is equal? Then In this line mid 
                                 'extracts from 13° char
                                 'to last char and set it on Text1  

   ElseIf mid(line$,1,10)="studentID=" then 'Is not equal to 
                                            ' "studentname="? Then 
                                            ' verify if the first 10 
                                            ' chars are equals to 
                                            ' "studentID="

    Text2.Text=mid(line$,11)     'Is equal? Then In this line mid 
                                 'extracts from 11° char
                                 'to last char and set it on Text2

    '... You can insert others ElseIf conditions 

   End If                         

  Loop                        'Loop

Close #1                      'This row closes file