none
Word searcher program RRS feed

  • Question

  • Hello.

    I would like to make a program, that searches for specific word(s).

    The complicated part would be, that i woold like it to be able to search by giving it specifications, as how long the word is, what letters does it have in it and most importantly, where the given letters are in the word.

    For example, if i know that my word is 5 letters and the first letter is "a" and the last one is "c", i can write in theese specififations like "a---c" and the program will search a given database for every 5 caracter long word that has "a" as first and "c" as last caracter. Ist this possible? If yes, do you have any ideas?

    Wednesday, January 30, 2019 7:51 PM

Answers

  • Hi

    Here is some code that may help. The emboldened line is probably all you need. This is a stand alone example, but needs a plain text file of single words (upper case) to load as base for search.

    Image

    Code

    ' Form1 with ListBox1, ListBox2
    ' TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' I load a list of 5 char
        ' words (UPPER CASE) for use in
        ' this example, into ListBox1
        ListBox1.DataSource = IO.File.ReadLines(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Words.txt")).ToList
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' enter a wildcarded string
        ' into TextBox1 and click
        ' Button1 to find matches.
        ListBox2.Items.Clear()
        For Each s As String In ListBox1.Items
          If s Like TextBox1.Text.ToUpper Then
            ListBox2.Items.Add(s)
          End If
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, January 30, 2019 8:45 PM
  • Hi Steve,

    I've made a sample. Does it satisfy your requirements?
        
    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Friday, February 1, 2019 8:08 AM
  • Hello,

    The following is done with SQL-Server, each of the DECLARE statements would become a parameter to a SqlClient Command which means the actual SQL SELECT is the CommandText of the command.

    Complete conceptual statement

    DECLARE @StartingChar AS NVARCHAR(MAX) = 'm'
    DECLARE @EndChar AS NVARCHAR(MAX) = 'u'
    DECLARE @MaximumLength AS INT = LEN('M------u')
    
    SELECT ProductID ,
           ProductName
    FROM   NorthWindAzure3.dbo.Products
    WHERE  LEFT(ProductName, 1) = @StartingChar
           AND RIGHT(ProductName, 1) = @EndChar
           AND LEN(ProductName) = @MaximumLength;

    So the code would be like the following which could also be done with a data reader which you loop thru results rather than a DataTable.

    Public Sub Demo(startsWith As String, endsWith As String, maxLenString As String)
        Dim dt As New DataTable
        Dim selectStatement As String =
                <SQL>
                SELECT ProductID ,
                       ProductName
                FROM   NorthWindAzure3.dbo.Products
                WHERE  LEFT(ProductName, 1) = @StartingChar
                       AND RIGHT(ProductName, 1) = @EndChar
                       AND LEN(ProductName) = @MaximumLength;
                </SQL>.Value
        Using cn = New SqlConnection(ConnectionString)
            Using cmd = New SqlCommand() With {.Connection = cn, .CommandText = ""}
                cmd.Parameters.AddWithValue("@StartingChar", startsWith)
                cmd.Parameters.AddWithValue("@EndChar", endsWith)
                cmd.Parameters.AddWithValue("@MaximumLength", maxLenString)
    
                cn.Open()
    
                dt.Load(cmd.ExecuteReader())
            End Using
    
        End Using
    End Sub

    Now I can see this going in different directions e.g. I want to get the first two characters and say the last two characters etc. that is of course new requirements thus a new question which more likely will get into TSQL logic.

    If this is not SQL-Server then the syntax may change slightly but all database SQL should have a left, right and len function in them.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, February 1, 2019 1:47 PM
    Moderator
  • If you replace ‘-‘ with ‘_’, then you can use the LIKE operator. For example, if textbox contains “--n-ai-s”, then you can perform the next query:

       SELECT * FROM MyTable WHERE MyColumn LIKE '__n_ai_s'


    Friday, February 1, 2019 6:03 PM

All replies

  • Hi

    Here is some code that may help. The emboldened line is probably all you need. This is a stand alone example, but needs a plain text file of single words (upper case) to load as base for search.

    Image

    Code

    ' Form1 with ListBox1, ListBox2
    ' TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' I load a list of 5 char
        ' words (UPPER CASE) for use in
        ' this example, into ListBox1
        ListBox1.DataSource = IO.File.ReadLines(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Words.txt")).ToList
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' enter a wildcarded string
        ' into TextBox1 and click
        ' Button1 to find matches.
        ListBox2.Items.Clear()
        For Each s As String In ListBox1.Items
          If s Like TextBox1.Text.ToUpper Then
            ListBox2.Items.Add(s)
          End If
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, January 30, 2019 8:45 PM
  • Hi Steve,

    I've made a sample. Does it satisfy your requirements?
        
    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Friday, February 1, 2019 8:08 AM
  • Hello,

    The following is done with SQL-Server, each of the DECLARE statements would become a parameter to a SqlClient Command which means the actual SQL SELECT is the CommandText of the command.

    Complete conceptual statement

    DECLARE @StartingChar AS NVARCHAR(MAX) = 'm'
    DECLARE @EndChar AS NVARCHAR(MAX) = 'u'
    DECLARE @MaximumLength AS INT = LEN('M------u')
    
    SELECT ProductID ,
           ProductName
    FROM   NorthWindAzure3.dbo.Products
    WHERE  LEFT(ProductName, 1) = @StartingChar
           AND RIGHT(ProductName, 1) = @EndChar
           AND LEN(ProductName) = @MaximumLength;

    So the code would be like the following which could also be done with a data reader which you loop thru results rather than a DataTable.

    Public Sub Demo(startsWith As String, endsWith As String, maxLenString As String)
        Dim dt As New DataTable
        Dim selectStatement As String =
                <SQL>
                SELECT ProductID ,
                       ProductName
                FROM   NorthWindAzure3.dbo.Products
                WHERE  LEFT(ProductName, 1) = @StartingChar
                       AND RIGHT(ProductName, 1) = @EndChar
                       AND LEN(ProductName) = @MaximumLength;
                </SQL>.Value
        Using cn = New SqlConnection(ConnectionString)
            Using cmd = New SqlCommand() With {.Connection = cn, .CommandText = ""}
                cmd.Parameters.AddWithValue("@StartingChar", startsWith)
                cmd.Parameters.AddWithValue("@EndChar", endsWith)
                cmd.Parameters.AddWithValue("@MaximumLength", maxLenString)
    
                cn.Open()
    
                dt.Load(cmd.ExecuteReader())
            End Using
    
        End Using
    End Sub

    Now I can see this going in different directions e.g. I want to get the first two characters and say the last two characters etc. that is of course new requirements thus a new question which more likely will get into TSQL logic.

    If this is not SQL-Server then the syntax may change slightly but all database SQL should have a left, right and len function in them.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, February 1, 2019 1:47 PM
    Moderator
  • If you replace ‘-‘ with ‘_’, then you can use the LIKE operator. For example, if textbox contains “--n-ai-s”, then you can perform the next query:

       SELECT * FROM MyTable WHERE MyColumn LIKE '__n_ai_s'


    Friday, February 1, 2019 6:03 PM
  • Thanks for all the answers, i got it to work!
    Saturday, February 2, 2019 9:00 PM
  • Hi Steve,
    I've shared my sample "WordSearch.zip" via OneDrive, which contains full project/solution.
    Please download and try it.
    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Saturday, February 2, 2019 11:09 PM
  • Thanks for all the answers, i got it to work!

    Hi

    All well and good that you 'got it to work', but of little help to anyone else. The better way is to include the code that you got to work as well so that anyone else looking for answers can see the result.


    Regards Les, Livingston, Scotland

    Sunday, February 3, 2019 12:22 AM