简体   繁体   English

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

[英]Check if each line from a text file contains a certain string, and add the ones that do to a listbox?

The following code works, to simply get each line as a line in the listbox. 下面的代码起作用,以简单地将每一行作为列表框中的一行。

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

But I don't want every line to go into the text box. 但是我不希望每一行都进入文本框。 I only want the lines that contain the text "Bookmark" to go into the listbox. 我只希望包含文本“书签”的行进入列表框。 What can I do to achieve this? 我该怎么做? I've tried everything I can think of. 我已经尝试了所有我能想到的。

Heres some code I tried, I can't see the problem in it, but it seems to just crash my program. 这是我尝试的一些代码,我看不到其中的问题,但这似乎只是使我的程序崩溃。

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

Try to change this line 尝试更改此行

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

to

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

Contains do a case sensitive comparison and your input string contains a lower case 'bookmark' 包含区分大小写的比较,并且您的输入字符串包含小写的“书签”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM