Results 1 to 6 of 6

Thread: VB.NET Search Text

  1. #1
    Gonzo District BOFH westin's Avatar
    Join Date
    Jan 2006
    Location
    SW MO
    Posts
    1,187

    VB.NET Search Text

    Hey Folks,

    I don't do much programming so excuse the newbie question[s]...

    I have a csv file that contains our DHCP reservations. I am looking for a good way to search for a particular string, and display the line of text that contains it.

    For example if I search for 10.1.2.3 in:

    c:\dhcp\dhcpres.csv

    10.1.2.0, 112233445566, pc-1,
    10.1.2.1, 223344556677, pc-2,
    10.1.2.2, 334455667788, pc-3,
    10.1.2.3, 445566778899, pc-4,

    I want the system to display:

    10.1.2.3, 445566778899, pc-4,

    in whatever field specified. Is there an easy way to do this in VB.NET?

    Thanks for any suggestions.
    \"Those of us that had been up all night were in no mood for coffee and donuts, we wanted strong drink.\"

    -HST

  2. #2
    Senior Member
    Join Date
    Apr 2005
    Location
    USA
    Posts
    422
    First you're going to want to use string.Split(Char(13)) or something to that effect to split your incoming string into an array of strings, one for each line. Then you're going to use string.Contains("10.1.2.3") which returns a bool that tells you whether the string contains that sub string.

    I personally recommend C# over VB, they both use the same libraries and object structures for the most part, however, in general, C# coders will get a somewhat higher pay for similar jobs. It all comes down to the perceived 'purpose' of the language even though they both can do the same exact thing. One will assume a C# coder has more experience, as C# is more related to the C/C++, which is used by most experts on large scale projects, while VB is often the first language a programmer learns, and is more related to simpler, business applications. But this all of course is just my opinion.

  3. #3
    Senior Member mungyun's Avatar
    Join Date
    Apr 2004
    Location
    Illinois
    Posts
    172
    Since you said you don't do much programming, i'll expand on mets post.
    here is probably close to what you want.

    Code:
    string ReservationFinder(string comp)
    {
        TextReader tr = new StreamReader("c:\file.csv");
        string input = null;
        while ((input = tr.ReadLine()) != null)
        {
            if(string.Contains(comp))
                return string;
        }
    }
    after that, you would do the Split() on the string to break each of the comma separated values into their own.

    Edit: oops, that was c#. here is the VB equivalent (May not be 100% right. havent done vb in ages)

    Code:
    Private Function ReservationFinder(comp As String) As String
    	Dim tr As TextReader = New StreamReader("c:\\file.csv")
    	Dim input As String = Nothing
    	Do While tr.Peek() <> -1
    		If String.Contains(comp) Then
    			Return String
    		End If
    	End While
    End Function
    Last edited by mungyun; August 11th, 2011 at 08:58 PM.
    I believe in making the world safe for our children, but not our children’s children, because I don’t think children should be having sex. -- Jack Handey

  4. #4
    Gonzo District BOFH westin's Avatar
    Join Date
    Jan 2006
    Location
    SW MO
    Posts
    1,187
    You guys are great! I will try this when I get home.

    I wouldn't mind learning C#, but I really don't code that much. Most of what I do is scripting... I think the last time I opened visual studio before I started this project was close to a year ago.

    Thanks a bunch for the suggestions and examples!
    \"Those of us that had been up all night were in no mood for coffee and donuts, we wanted strong drink.\"

    -HST

  5. #5
    @ÞΜĮЙǐЅŦГǻţΩЯ D0pp139an93r's Avatar
    Join Date
    May 2003
    Location
    St. Petersburg, FL
    Posts
    1,705
    VBScript with MS Office supports calling Excel functionality for searching and managing csv files.

    I used that for a very similar script that managed MFDs across a network.

    If I can find it, I'll post it so you can see the inner workings.
    Real security doesn't come with an installer.

  6. #6
    Gonzo District BOFH westin's Avatar
    Join Date
    Jan 2006
    Location
    SW MO
    Posts
    1,187
    Sounds interesting. Once again, all of the help is most appreciated.
    \"Those of us that had been up all night were in no mood for coffee and donuts, we wanted strong drink.\"

    -HST

Similar Threads

  1. Unraveling Google
    By d00dz Attackin in forum Other Tutorials Forum
    Replies: 21
    Last Post: December 6th, 2005, 10:20 AM
  2. Search Engine Ranking: Being On Top Of It
    By scratchONtheBOX in forum Other Tutorials Forum
    Replies: 6
    Last Post: March 30th, 2005, 04:53 PM
  3. Use the power of Search Engines
    By d00dz Attackin in forum Other Tutorials Forum
    Replies: 22
    Last Post: November 20th, 2004, 10:49 AM
  4. The history of the Mac line of Operating systems
    By gore in forum Operating Systems
    Replies: 3
    Last Post: March 7th, 2004, 08:02 AM
  5. Help with PHP
    By Euclid in forum AntiOnline's General Chit Chat
    Replies: 3
    Last Post: March 6th, 2003, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •