Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > string parsing


Reply
 
Thread Tools Display Modes
  #1  
Old 07-11-2006, 02:38 PM
mookie mookie is offline
Centurion
 
Join Date: Feb 2004
Posts: 179
Default string parsing


Im just fishing for different ways that some of you might go about the following task. More so because im only a novice and i can do it but i would like to know if there is a different way.


I have a tabcontrol that i am going to use as an config tool for different applications. the actual parameters are listed in a textfile "$config.dat"


im searching for different things such as the string


"DISP1SCHEDULE[1]={DISP_GUN 1,COMMENT[]"




now heres the thing..

this file that im using has 2187 lines within it. normally i have created a richtextbox to load the file into and then parse it from there such as

Code:
Private Sub AppConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Stop Dim temp As String If File.Exists(pFilepath & "\KRC\R1\System\$config.dat") Then Dim cText As New RichTextBox cText.LoadFile(pFilepath & "\KRC\R1\System\$config.dat", RichTextBoxStreamType.PlainText) temp = cText.Text.Substring(cText.Text.LastIndexOf("DISP1SCHEDULE[1]={DISP_GUN 1,COMMENT[] "), cText.Text.Length - cText.Text.LastIndexOf("DISP1SCHEDULE[32]={DISP_GUN 1,COMMENT[] ")) For i As Integer = 1 To 32 cText.Find("DISP1SCHEDULE[1]={DISP_GUN 1,COMMENT[]") Next End If end sub

but i have seen alot of people use a streamreader such as

Code:
If File.Exists(pFilepath & "\KRC\R1\System\$config.dat") Then Dim nfs As FileStream = File.OpenRead(pFilepath & "\KRC\R1\System\$config.dat") Do While nfs.Read(b, 0, b.Length) > 0 ' do code to find parameters 'Set StatusBarText Loop


is there advantages of using one over the other?

Last edited by mookie; 07-11-2006 at 02:39 PM. Reason: ...
Reply With Quote
  #2  
Old 07-11-2006, 08:20 PM
brandnew's Avatar
brandnew brandnew is offline
Centurion
 
Join Date: Jun 2004
Location: Phnom Penh, Cambodia.
Posts: 181
Default

Regular Expression (a.k.a Regex) rules them all! Read The 30 minute regex Tutorial

First back to the two, you did it all wrong with the RichTextBox. You don't need it just to read file. What you really need is:
VS 2005
Code:
Dim text As String = IO.File.ReadAllText(pFilepath & "\KRC\R1\System\$config.dat")
VS 2003
Code:
Dim sr As IO.StreamReader = New IO.StreamReader(pFilepath & "\KRC\R1\System\$config.dat") Dim text As String = sr.ReadToEnd
__________________
Very useful and free stuffs for VB programmers.
API-Guide - Wonderful API viewer with examples and full explanation.
APIViewer - More APIs; replacement for default api viewer in VB.
Reply With Quote
  #3  
Old 07-11-2006, 11:35 PM
gigemboy's Avatar
gigemboy gigemboy is offline
Centurion
 
Join Date: Oct 2005
Location: College Station, TX
Posts: 156
Default

Quote:
Originally Posted by brandnew
VS 2003
Code:
Dim sr As IO.StreamReader = New IO.StreamReader(pFilepath & "\KRC\R1\System\$config.dat") Dim text As String = sr.ReadToEnd
Dont forget to close the streamreader with "sr.Close"

Regex is great if you know the syntax, but it takes a while to learn it in order to be able to use it effectively, but I just love it... (get that tutorial link from me?? I post that everywhere hehe)
__________________
NOTE: My code will usually get the job done, but it is not the prettiest. If you see of a way to make it more efficient or to look more elegant, please feel free to comment on it :) Im still learning...
Reply With Quote
  #4  
Old 07-12-2006, 05:52 AM
shaul_ahuva shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Quote:
Originally Posted by mookie
...is there advantages of using one over the other?
If you use a StreamReader, you don't have the extra overhead of the RichTextBox
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->