Jay1b Posted August 23, 2003 Posted August 23, 2003 I am using the below code to find the character number for 'Cblah3', and then find the next space after 'Cblah3', then using substring to get the text in the middle. This works brilliantly - what i dont know is how to find at what character the end of the line is, so i could use either the next space or the end of the line which ever is nearer. The testfile is posted at the bottom of the post. Thanks in advance for any help. Private Sub BlahTestButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlahTestButton.Click Dim reader As New System.IO.StreamReader("c:\blahtest.txt") Dim textfromfile As String = reader.ReadToEnd() 'use the ReadToEnd() method to read the whole file Dim ResultStart As Integer Dim ResultEnd As Integer Dim Results As String ResultStart = (InStr(textfromfile, "Cblah3")) MsgBox(ResultStart) 'Starts searching for a space after the first find was found, 'the +1 is needed to ensure it searches after the first find. ResultEnd = InStr(ResultStart + 1, textfromfile, " ") MsgBox(ResultEnd) 'Finds the text between the characters references, the (ResultEnd - ResultStart) 'is required to work out the difference and just take the text for that section Results = textfromfile.Substring(ResultStart, (ResultEnd - ResultStart)) MsgBox(Results) 'Returns the length of the string, to compare to the actual length to see 'if theres a space in it or not MsgBox(Results.Length) End Sub Ablah1 Ablah2 Ablah3 Bblah1 Bblah2 Bblah3 Cblah1 Cblah2 Cblah3 Dblah1 Dblah2 Dblah3 Eblah1 Eblah2 Eblah3 Fblah1 Fblah2 Fblah3 Gblah1 Gblah2 Gblah3 Hblah1 Hblah2 Hblah3 Quote
wyrd Posted August 23, 2003 Posted August 23, 2003 A string is nothing but an array of characters, so you can get the last character in the string like this; For VB: char = string.Chars(string.Length - 1) For C#: char = string[string.Length - 1]; Quote Gamer extraordinaire. Programmer wannabe.
Jay1b Posted August 23, 2003 Author Posted August 23, 2003 I am after the end of a line in my posted textfile, not the end of the entire string. Quote
wyrd Posted August 23, 2003 Posted August 23, 2003 Oh. :P Easiest way to do that would probably be with regular expressions. http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemTextRegularExpressions.asp?frame=true Quote Gamer extraordinaire. Programmer wannabe.
Jay1b Posted August 23, 2003 Author Posted August 23, 2003 Thanks again, but i think i should explain exactly what i am trying to do. I am trying to write a program that will read the windows hosts file taking each IP address and domain name and putting them into separate table coloumns within VB. As names are variable in length and most would not have space separately them from the IP address on the next line, i need to find what character the 'return carriage' falls on. Or at least find the last character before the end of line, or find a way of doing a substring until the end of the line. Obviously what i am doing now is just learning the tools i will need later one. Quote
wyrd Posted August 23, 2003 Posted August 23, 2003 You could probably just use String.Split() and split based on \n\r. But it looks like you're going through a lot of coding to get this to work when a single regular expression would do all the work for you. I'd suggest learning regular expressions since you're just doing this for learning purposes. The String methods are very limited in comparison. Quote Gamer extraordinaire. Programmer wannabe.
Jay1b Posted August 23, 2003 Author Posted August 23, 2003 Thanks i shall look into it, i found a way to do it though, using Chr(10), in other words searching for the next linefeed or Chr(11) for carriage return. I shall look into string.split, before i go any further though. Cheers. Quote
wyrd Posted August 23, 2003 Posted August 23, 2003 Yeah, \n\r is chr(10) and chr(11). You can also use Environment.NewLine which takes care of both at once. Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Nerseus Posted August 23, 2003 *Experts* Posted August 23, 2003 They should 10 and 13, not 10 and 11. If you're parsing a log file and you want to grab individual bits of information, such as an IP, datetime and a message, I'd consider regular expressions. They're a bit slower than using Split or Substring, but they're much more powerful. You could create an expression that essentially gives you the parameters in a named fashion using Groups and Matches. The code would be MUCH more readable than a bunch of substrings or Splits, but at the expense of some speed. Unless the speed is just horrible, I'd always go with more readable code. Of course, if you don't want to learn regular expressions, that would be another reason to not go that route. Me, if I hear that there's code out there that's *made* to do something that I need, I like to invest a couple of hours and figure it out to see if it is what they say it is. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Jay1b Posted August 23, 2003 Author Posted August 23, 2003 I am going to look into regular expressions tomorrow. Any pointers with where i should start looking? Thanks. Quote
wyrd Posted August 23, 2003 Posted August 23, 2003 Oh.. right.. 13. That's what I ment, honest. :D Jay: I wrote this ages ago when I was first toying with .NET, but it should provide a decent place to start. http://www.xtremedotnettalk.com/showthread.php?s=&threadid=49579 Also check out the link I posted above to the MSDN Library. Quote Gamer extraordinaire. Programmer wannabe.
*Experts* mutant Posted August 23, 2003 *Experts* Posted August 23, 2003 There are some Regular Expression samples on GotDotNet: http://www.gotdotnet.com/community/usersamples/Default.aspx?query=regular%20expressions Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.