检查文本文件中的每一行是否包含某个字符串,并将那些行添加到列表框中?

时间:2013-02-01 13:21:44

标签: vb.net

以下代码可以简单地将每一行作为列表框中的一行。

Reader = IO.File.OpenText(textlocation)
Dim bookmarks() As String = Reader.ReadToEnd.Split(vbNewLine)
Dim i As Integer = 0

Do Until i = bookmarks.Length
  lstFavorites.Items.Add(bookmarks(i))
  i += 1
Loop

但我不希望每一行都进入文本框。我只希望包含文本“Bookmark”的行进入列表框。我能做些什么来实现这个目标?我已经尝试了我能想到的一切。

继承了我试过的一些代码,我看不到它的问题,但它似乎只是让我的程序崩溃。

Do Until i = bookmarks.Length
  If bookmarks(i).Contains("at") Then
    If radBookmarks.Checked Then
      If bookmarks(i).Contains("Bookmark") Then
        Original = bookmarks(i)
        BeginningOfDemoName = Original.Substring(Original.LastIndexOf("(") + 2)
        TickWithParenthesis = BeginningOfDemoName.Substring(BeginningOfDemoName.IndexOf(Chr(34)) + 4)
        Tick = TickWithParenthesis.Split(" ")(1).Split(")")(0)
        DemoName = BeginningOfDemoName.Split(Chr(34))(0)
        ToList = DemoName + " at " + Tick
        lstFavorites.Items.Add(ToList)
        i += 1
      Else
        i += 1
      End If
    ElseIf radEverything.Checked Then
      Original = bookmarks(i)
      BeginningOfDemoName = Original.Substring(Original.LastIndexOf("(") + 2)
      TickWithParenthesis = BeginningOfDemoName.Substring(BeginningOfDemoName.IndexOf(Chr(34)) + 4)
      Tick = TickWithParenthesis.Split(" ")(1).Split(")")(0)
      DemoName = BeginningOfDemoName.Split(Chr(34))(0)
      ToList = DemoName + " at " + Tick

      lstFavorites.Items.Add(ToList)
      i += 1
    End If
  End If
Loop

1 个答案:

答案 0 :(得分:0)

尝试更改此行

 If bookmarks(i).Contains("Bookmark") Then

 If bookmarks(i).IndexOf("Bookmark", 
                 StringComparison.CurrentCultureIgnoreCase) >= 0 Then

包含进行区分大小写的比较,输入字符串包含小写“书签”