检查文件逻辑中的第一个单词

时间:2018-03-28 18:11:12

标签: vb.net vb.net-2010

我正在尝试在打开文件的程序中创建一个部分,如果第一行的第一个单词是" title"然后我希望它将输入组写入文件,而不是标题行。如果文件为空并且没有第一个单词" title"然后我想让它写一次该行,然后继续将其他输入框写入文本文件。我不认为我太遥远了,我只需要另外一套关注我的逻辑。

谢谢!

我的输出是:

[Car]{bgcolor: ##fcecec}
*VIN{label: varchar, not null}
Make {label: varchar, not null}
Model {label: varchar, not null}
Year {label: varchar, not null}
Color {label: varchar, not null}
Miles {label: varchar, not null}

title {label: Database (condensed), size: 20}
[Owner]{bgcolor: ##ececfc}
*Fname{label: varchar, not null}
LName {label: varchar, not null}
Address {label: varchar, not null}
Phone {label: varchar, not null}
Employeer {label: varchar, not null}
Annual Income {label: varchar, not null}
Married {label: varchar, not null}
DOB {label: varchar, not null}

title {label: Database (condensed), size: 20}
[Employeer]{bgcolor: ##d0e0d0}
*Name{label: varchar, not null}
Address {label: varchar, not null}
Phone {label: varchar, not null}
Salary {label: varchar, not null}
Years with company {label: varchar, not null}
Job title {label: varchar, not null}

它应该是什么样的:

title {label: Database (condensed), size: 20}
[Car]{bgcolor: ##fcecec}
*VIN{label: varchar, not null}
Make {label: varchar, not null}
Model {label: varchar, not null}
Year {label: varchar, not null}
Color {label: varchar, not null}
Miles {label: varchar, not null}

[Owner]{bgcolor: ##ececfc}
*Fname{label: varchar, not null}
LName {label: varchar, not null}
Address {label: varchar, not null}
Phone {label: varchar, not null}
Employeer {label: varchar, not null}
Annual Income {label: varchar, not null}
Married {label: varchar, not null}
DOB {label: varchar, not null}

[Employeer]{bgcolor: ##d0e0d0}
*Name{label: varchar, not null}
Address {label: varchar, not null}
Phone {label: varchar, not null}
Salary {label: varchar, not null}
Years with company {label: varchar, not null}
Job title {label: varchar, not null}

我的代码是:

 'database name

    ' Create an instance of StreamReader to read from a file.
    ' The using statement also closes the StreamReader.
    Using sr As New StreamReader(FILE_NAME)
        Dim line, firstWord As String
        Dim k As Integer = 0
        ' Read and display lines from the file until the end of
        ' the file is reached.
        Do
            line = sr.ReadLine()

            If Not (line Is Nothing) And k = 0 Then
                firstWord = line.Split(" ")(k)
                If firstWord.Contains("title") Then
                    sr.Close() ' closes file
                    GoTo AttributeWrite
                Else
                    sr.Close() ' closes file
                    Dim TitleWrite As New System.IO.StreamWriter(FILE_NAME, True) ' open file
                    TitleWrite.WriteLine("title {label: " & TextBox10.Text & " (condensed)" & ", size: " & "20" & "}") ' write to file
                    TitleWrite.Close() ' closes file


                End If
            End If
        Loop Until k = 0
    End Using

2 个答案:

答案 0 :(得分:0)

如果是文本文件,请使用File.ReadAllLines。创建字符串列表,然后删除索引(0)处的字符串项。样本:

 Dimm AllData as new List(Of String)

 For each Line in FIle.ReadAllLines(filepathhere)
 AllData.Add(Line)
 Next
 AllData.RemoveAt(0) 'This will remove the first line only

如果title不在第一行,请找到index,然后将其删除:

Dim index As Integer = AllData.FindIndex(Function(a) a = "title")
AllData.RemoveAt(Index)

希望这会有所帮助:)。

答案 1 :(得分:0)

休息一下再看一遍后,我发现了我遇到问题的地方。

如果"我必须移动我的位置。

感谢那些提供解决方案的人:)

 If Not (line Is Nothing) And k = 0 Then
                firstWord = line.Split(" ")(k)
                If firstWord.Contains("title") Then
                    sr.Close() ' closes file
                    GoTo AttributeWrite
                End If  <----- THIS ONE

                Else
                    sr.Close() ' closes file
                    Dim TitleWrite As New 
  System.IO.StreamWriter(FILE_NAME, True) ' open file
                    TitleWrite.WriteLine("title {label: " & 
  TextBox10.Text & " (condensed)" & ", size: " & "20" & "}") ' write to file
                    TitleWrite.Close() ' closes file


                End If