4 Replies - 15326 Views - Last Post: 23 January 2007 - 08:08 PM Rate Topic: -----

#1 Beng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 01-January 07

Reading between two characters in a text file

Posted 18 January 2007 - 09:49 PM

Hi I don;t knwo if anyone has a solution for this problem but i need some code to read and extract the data between two characters in VB.NET. This one has really stumped me and i would just like part of the code because i can prob do the rest. Any help would be greatly appreciated. Thanks in advance!!!
Is This A Good Question/Topic? 0
  • +

Replies To: Reading between two characters in a text file

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Reading between two characters in a text file

Posted 21 January 2007 - 10:55 AM

Assuming those two characters are part of String object, you can pinpoint the locations of those characters using the InStr() method, and then return what you need using the .Substring() method of the string object using the two positional coordinates obtained from the first part.
Was This Post Helpful? 0
  • +
  • -

#3 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Reading between two characters in a text file

Posted 21 January 2007 - 01:36 PM

View PostBeng, on 18 Jan, 2007 - 09:49 PM, said:

Hi I don;t knwo if anyone has a solution for this problem but i need some code to read and extract the data between two characters in VB.NET. This one has really stumped me and i would just like part of the code because i can prob do the rest. Any help would be greatly appreciated. Thanks in advance!!!


Use mid() functon.

Hope it helps :D
Was This Post Helpful? 0
  • +
  • -

#4 Beng  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 01-January 07

Re: Reading between two characters in a text file

Posted 23 January 2007 - 03:59 PM

Thanks i am grateful for the response but with the mid function how do you get it to move down one line when reading say the mid can't find a certian character in that line so it automatically moves down one line?

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#5 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: Reading between two characters in a text file

Posted 23 January 2007 - 08:08 PM

View PostBeng, on 23 Jan, 2007 - 03:59 PM, said:

Thanks i am grateful for the response but with the mid function how do you get it to move down one line when reading say the mid can't find a certian character in that line so it automatically moves down one line?

Thanks again!

Was there a question made? Or even many in 1?
1) How to use multi line mid
2) Can Mid find a certain character in line, so it would understand to take next line
3) Can i read data in with mid, at certain part of line

Chop it up before, and then you can move any way you want. I made an example that i think would answer your question.

	Private Function Lose_The_Log_Date(Optional ByVal MyFileName As String = "C:\MyData.ptf") As String
		Lose_The_Log_Date = ""

		Try
			For Each x As String In My.Computer.FileSystem.ReadAllText(MyFileName).Split(Chr(13))
				Lose_The_Log_Date &= Chr(13) & Mid(x, 10, 30)
			Next x
		Catch ex As IO.IOException
			MsgBox("Be brave; There Was An Error")
		End Try

	End Function


And second example is for showing how to read in data between some string.
	Private Overloads Function Open_Tag(ByVal DataToBeInspected As String, _
										ByVal BetweenWhat As String) As String
		Dim Turn_To_Find_Opening As Boolean = True
		Open_Tag = ""

		For i As Integer = 1 To Len(DataToBeInspected)

			Select Case Turn_To_Find_Opening
				Case True
					If Mid(DataToBeInspected, i, Len(BetweenWhat)) = BetweenWhat Then
						Turn_To_Find_Opening = Not Turn_To_Find_Opening
					End If
				Case Else
					If Mid(DataToBeInspected, i, Len(BetweenWhat)) = BetweenWhat Then
						Turn_To_Find_Opening = Not Turn_To_Find_Opening
						Open_Tag &= "|"
					Else
						Open_Tag &= Mid(DataToBeInspected, i, 1)
					End If
			End Select

		Next i

	End Function
	Private Overloads Function Open_Tag(ByVal DataToBeInspected As String, _
										ByVal Starting As String, _
										ByVal Ending As String) As String
		Dim Turn_To_Find_Opening As Boolean = True
		Open_Tag = ""

		For i As Integer = 1 To Len(DataToBeInspected)

			Select Case Turn_To_Find_Opening
				Case True
					If Mid(DataToBeInspected, i, Len(Starting)) = Starting Then
						Turn_To_Find_Opening = Not Turn_To_Find_Opening
					End If
				Case Else
					If Mid(DataToBeInspected, i, Len(Ending)) = Ending Then
						Turn_To_Find_Opening = Not Turn_To_Find_Opening
						Open_Tag &= "|"
					Else
						Open_Tag &= Mid(DataToBeInspected, i, 1)
					End If
			End Select

		Next i

	End Function

If you want to test how does it work, then you can test it with:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim tekst As String = _
		"/* How does it work? " & Chr(13) & _
		"Lorem ipsum" & Chr(13) & _
		"Lorem ipsum" & Chr(13) & _
		"Lorem ipsum*/" & Chr(13) & Chr(13) & _
		"And now some content." & Chr(13) & _
		"/* How does it work 2? " & Chr(13) & _
		"Lorem ipsum" & Chr(13) & _
		"Lorem ipsum" & Chr(13) & _
		"Lorem ipsum*/" & Chr(13) & Chr(13) & _
		"And now some content2."
		Dim a As String = Open_Tag(tekst, "/*", "*/")

		MsgBox(tekst)
		MsgBox(a)
	End Sub


Hope it helped alot :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1