[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1256
  • Last Modified:

Search strings in an array of strings

HI all,

I am wanting to search an array of strings for a word or phrase, when it has found a valid string in the array I would then like it to put this into another string array.. So I can then export it all into excel...
EG..
dim strOriginalStringArray (5) as string

strOriginalStringArray (1) = "Hello"
strOriginalStringArray (2) = "How are you?"
strOriginalStringArray (3) = "Yes i am ok thank you and you??"
strOriginalStringArray  (4) = "I am ok thanks"
strOriginalStringArray (5) = "So what are you up to?"

so then if i search for the word "you" in a textbox, I would then end up with:

dim strSearchedArray as string
strSearchedArray (1) = "How are you?"
strSearchedArray (2) = "Yes i am ok thank you and you??"
strSearchedArray (3) = "So what are you up to?"

Obviously all the strings in the array that have the word "you" in them have now been put into a different array ready to be exported into Excel


thanks

0
bignig32
Asked:
bignig32
  • 2
  • 2
1 Solution
 
iboutchkineCommented:
Sub TestArraySearches()
        Dim strArr() As String = {"Robert", "Joe", "Ann", "Chris", "Joe"}
        ' search for the "Ann" value
        Console.WriteLine(Array.IndexOf(strArr, "Ann"))   ' => 2
        ' Note that string searches are case-sensitive.
        Console.WriteLine(Array.IndexOf(strArr, "ANN"))   ' => -1

        ' searches for all the occurrences of the "Joe" string.
        Dim index As Integer = -1
        Do
            ' Search next occurrence.
            index = Array.IndexOf(strArr, "Joe", index + 1)
            ' Exit the loop if not found.
            If index = -1 Then Exit Do
            Console.WriteLine("Found at index " & index.ToString)
        Loop

        ' A revised version of the search loop, that searches
        ' from higher indices towards the beginning of the array.
        index = strArr.Length
        Do
            index = Array.LastIndexOf(strArr, "Joe", index - 1)
            If index = -1 Then Exit Do
            Console.WriteLine("Found at index " & index.ToString)
        Loop

        ' Binary search on a sorted array
        Dim strArr2() As String = {"Ann", "Chris", "Joe", "Robert", "Sam"}
        Console.WriteLine(Array.BinarySearch(strArr2, "Chris"))  ' => 1

        ' an unsuccessful search
        index = Array.BinarySearch(strArr2, "David")
        If index >= 0 Then
            Console.WriteLine("Found at index " & index.ToString)
        Else
            ' Negate the result to get the index for insertion point.
            index = Not index
            Console.WriteLine("Not Found. Insert at index " & index.ToString)
            ' => Not found. Insert at index 2
        End If

    End Sub
0
 
bignig32Author Commented:
HI,
thanks iboutchkine  for the quick response,
i have tried this in my code.. however it only seems to find complete lines for example:

dim strOriginalStringArray (5) as string

strOriginalStringArray (1) = "Hello"
strOriginalStringArray (2) = "How are you?"
strOriginalStringArray (3) = "Yes i am ok thank you and you??"
strOriginalStringArray  (4) = "I am ok thanks"
strOriginalStringArray (5) = "So what are you up to?"
'and i did:
   Do
            ' Search next occurrence.
            index = Array.IndexOf(strOriginalStringArray, "you", index + 1)
            ' Exit the loop if not found.
            If index = -1 Then Exit Do
            Console.WriteLine("Found at index " & index.ToString)
        Loop

it would return no results because the indexof property will only find complete lines that are the same not just one word that is in the line and then  select its index, wich is what i am trying to do,
also one more thing, how could i mean the searches NOT case sensati


thanks
Matt
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try this out:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.ListBox2 = New System.Windows.Forms.ListBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(224, 152)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(56, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Search"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(40, 152)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(176, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "you"
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(8, 24)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(272, 121)
        Me.ListBox1.TabIndex = 2
        '
        'ListBox2
        '
        Me.ListBox2.Location = New System.Drawing.Point(8, 200)
        Me.ListBox2.Name = "ListBox2"
        Me.ListBox2.Size = New System.Drawing.Size(272, 108)
        Me.ListBox2.TabIndex = 3
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(272, 16)
        Me.Label1.TabIndex = 4
        Me.Label1.Text = "Source Array:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 152)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(32, 24)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Find:"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 184)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(272, 16)
        Me.Label3.TabIndex = 6
        Me.Label3.Text = "Matches:"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(288, 318)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.ListBox2)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private strOriginalStringArray(4) As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        strOriginalStringArray(0) = "Hello"
        strOriginalStringArray(1) = "How are you?"
        strOriginalStringArray(2) = "Yes i am ok thank you and you??"
        strOriginalStringArray(3) = "I am ok thanks"
        strOriginalStringArray(4) = "So what are you up to?"

        Dim strValue As String
        ListBox1.BeginUpdate()
        For Each strValue In strOriginalStringArray
            ListBox1.Items.Add(strValue)
        Next
        ListBox1.EndUpdate()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strValue As String
        Dim matches As ArrayList = findMatches(strOriginalStringArray, TextBox1.Text)

        ListBox2.BeginUpdate()
        ListBox2.Items.Clear()
        For Each strValue In matches
            ListBox2.Items.Add(strValue)
        Next
        ListBox2.EndUpdate()
    End Sub

    Private Function findMatches(ByVal sourceArray() As String, ByVal findValue As String) As ArrayList
        Dim strValue As String
        Dim matches As New ArrayList
        For Each strValue In sourceArray
            If strValue.IndexOf(findValue) <> -1 Then
                matches.Add(strValue)
            End If
        Next
        Return matches
    End Function

End Class
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you do not want it to be case sensitive then you can change the findMatches() function to convert everything to lowercase like this:

    Private Function findMatches(ByVal sourceArray() As String, ByVal findValue As String) As ArrayList
        findValue = findValue.ToLower
        Dim strValue As String
        Dim lowerValue As String
        Dim matches As New ArrayList
        For Each strValue In sourceArray
            lowerValue = strValue.ToLower
            If lowerValue.IndexOf(findValue) <> -1 Then
                matches.Add(strValue)
            End If
        Next
        Return matches
    End Function

~IM
0
 
iboutchkineCommented:
YOu can check every member of aray and search for substring

Dim r As String =  "How are you?"
r = r.IndexOf("you")

You are rignt. IndexOf is case sensitive.
If you want to do it case insensitive , I think you would be better of using  Reg Expressions

A way to specify case insensitivity is to add (?i) to the beginning of the pattern.

   ^(?i)[a-z]{3}$

This expression will match abc, AbC, and ABC.

-------------------------------
Here is the short example how to find you or You

Imports System.Text.RegularExpressions

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(0, 0)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' This code requires the following imports
        '  Imports System.Text.RegularExpressions

        Dim s As String = "Hi there, how are You?"
        Dim re As New Regex("\w+")
        Dim m As Match
        For Each m In re.Matches(s)
            If m.Value = "you" OrElse m.Value = "You" Then
                Console.WriteLine(m.Value)
            End If
        Next
    End Sub
End Class
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now