-
August 9th, 2011, 11:27 PM
#1
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
-
August 11th, 2011, 12:34 PM
#2
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.
-
August 11th, 2011, 08:53 PM
#3
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
-
August 11th, 2011, 09:20 PM
#4
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
-
August 11th, 2011, 11:29 PM
#5
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.
-
August 12th, 2011, 02:16 AM
#6
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
-
By d00dz Attackin in forum Other Tutorials Forum
Replies: 21
Last Post: December 6th, 2005, 10:20 AM
-
By scratchONtheBOX in forum Other Tutorials Forum
Replies: 6
Last Post: March 30th, 2005, 04:53 PM
-
By d00dz Attackin in forum Other Tutorials Forum
Replies: 22
Last Post: November 20th, 2004, 10:49 AM
-
By gore in forum Operating Systems
Replies: 3
Last Post: March 7th, 2004, 08:02 AM
-
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
-
Forum Rules
|
|