none
I need the first word of each line of a text file RRS feed

  • Question

  • I am making a program which acts like a dictionary. there's a text file which has 5000 definitions. the current code is working great and it is fast but there are certain words that are in other definitions so to fix this I want the program to read only the first word of each line. What I thought about doing is making the program read the next line whenever it reads a space but I don't know how to go upon doing that. The program is currently fast and I would like to keep it that way. here's a sample line from the text file "zodiac n. an imaginary belt encircling the heavens within which are the larger planets."


    This is the code I have right now - 

    Imports System.IO
    Public Class Form
        Private Sub cmdFind_Click(sender As Object, e As EventArgs) Handles cmdFind.Click
            Dim lines() As String = IO.File.ReadAllLines("E:\Private\VB personel\Dictionary\Dictionary\bin\Dictionary5000.txt")
            txtDictionary.Text = lines(0)
            Dim search As String = LCase(txtSearch.Text)
            Dim read As String = File.ReadAllText("E:\Private\VB personel\Dictionary\Dictionary\bin\Dictionary5000.txt")
            If search = "list_of_all" Then
                txtDictionary.Text = read
            End If
            Using reader As New StreamReader("E:\Private\VB personel\Dictionary\Dictionary\bin\Dictionary5000.txt")
                While Not reader.EndOfStream
                    Dim line As String = reader.ReadLine()
                    If line.Contains(search) Then
                        txtDictionary.Text = line
                        Exit While
                    End If
                End While
            End Using
        End Sub
    End Class



    Saturday, March 11, 2017 5:32 PM

Answers

  • I want the program to read only the first word of each line.

    Change this:
                    If line.Contains(search) Then

    to this
                    If line.StartsWith(search) Then

    That way you are not restricting your search term to a single word, but you are still only finding lines where the search term is actually defined as the first part of the line, not just mentioned anywhere in the line.

    • Marked as answer by BadNewsHughes Saturday, March 11, 2017 8:51 PM
    Saturday, March 11, 2017 8:50 PM

All replies

  • Hi

    How about using a Dictionary which is made for uses such as this.

    Here is one example of use for a Dictionary

    ' New Form1 with only a Label1 on it
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myDictionary As New Dictionary(Of String, String)
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            ' here just loads one item into dictionary, you
            ' would add items from your file.
            myDictionary.Add("zodiac", "n. an imaginary belt encircling the heavens within which are the larger planets.")
    
            ' this illustrates using Dictionary by key
            Label1.Text = myDictionary("zodiac")
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by BadNewsHughes Saturday, March 11, 2017 8:50 PM
    • Unmarked as answer by BadNewsHughes Saturday, March 11, 2017 8:52 PM
    Saturday, March 11, 2017 5:41 PM
  • I want the program to read only the first word of each line.


    Perhaps something like this example:

    Dim line As String = "This is a test string"
    Dim search As String = "This"
    If (line.Substring(0, search.Length) = search) Then
        Console.WriteLine("It's a match!")
    End If
    

    - Wayne

    Saturday, March 11, 2017 7:39 PM
  • I want the program to read only the first word of each line.

    Change this:
                    If line.Contains(search) Then

    to this
                    If line.StartsWith(search) Then

    That way you are not restricting your search term to a single word, but you are still only finding lines where the search term is actually defined as the first part of the line, not just mentioned anywhere in the line.

    • Marked as answer by BadNewsHughes Saturday, March 11, 2017 8:51 PM
    Saturday, March 11, 2017 8:50 PM
  • Thank you this worked perfectly!
    Saturday, March 11, 2017 8:53 PM