Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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];

Gamer extraordinaire. Programmer wannabe.
Posted

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.

Posted
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.
Gamer extraordinaire. Programmer wannabe.
Posted

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.

Posted
Yeah, \n\r is chr(10) and chr(11). You can also use Environment.NewLine which takes care of both at once.
Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted

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

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...