Using the Split function to detect certain lines of code?

  • Thread starter Thread starter RedFox
  • Start date Start date
R

RedFox

Hello, I'm a beginner at programming and started working on a specific
console program that uses StreamReader to go through the code and find
required data, then check if lines Contain or StartWith with certain strings.

The problem is that the current split method to separate Data isn't very
efficient if the text has tabs and spaces in it, instead of just tabs for
example. Is there a way to properly identify a specific section of a line,
while using only one Sub (Sub Main()), since I'm not very skilled at that yet
and the whole program is a set of GoTo loops until everything is finished.

Here's what the line looks like:
faction seleucid ; The Seleucid Empire
And the empty characters can occur pretty randomly:

faction[TAB][TAB][randomspaces][TAB][space]seleucid[space]; The Seleucid
Empire

Here's the initializing piece of code, where [sr] is the streamreader for
the correct .txt file:
Do
line = sr.ReadLine()
If line Is Nothing Then
GoTo FinishedAll
End If
Loop Until line.StartsWith("faction")

So far I've used split functions, but I'm not sure that's the right way.
What I need is for it to always correctly retrieve "seleucid" as String
CurrentFaction and ignore all text after a comment mark ";".

Hope you can help me out with this! I really love programming so far, but
Subs still leave me confused and all the more complex functions require
really good knowledge of how Subs act, which I don't have.

Regards,
- RedFox
 
RedFox said:
Hello, I'm a beginner at programming and started working on a specific
console program that uses StreamReader to go through the code and find
required data, then check if lines Contain or StartWith with certain strings.

The problem is that the current split method to separate Data isn't very
efficient if the text has tabs and spaces in it, instead of just tabs for
example. Is there a way to properly identify a specific section of a line,
while using only one Sub (Sub Main()), since I'm not very skilled at that yet
and the whole program is a set of GoTo loops until everything is finished.

Here's what the line looks like:
faction seleucid ; The Seleucid Empire
And the empty characters can occur pretty randomly:

faction[TAB][TAB][randomspaces][TAB][space]seleucid[space]; The Seleucid
Empire

Here's the initializing piece of code, where [sr] is the streamreader for
the correct .txt file:
Do
line = sr.ReadLine()
If line Is Nothing Then
GoTo FinishedAll
End If
Loop Until line.StartsWith("faction")

So far I've used split functions, but I'm not sure that's the right way.
What I need is for it to always correctly retrieve "seleucid" as String
CurrentFaction and ignore all text after a comment mark ";".

Hope you can help me out with this! I really love programming so far, but
Subs still leave me confused and all the more complex functions require
really good knowledge of how Subs act, which I don't have.

Regards,
- RedFox

Based on what you have so far, if you did:

dim Tokens as char () = {" "c, vbTab, ";"c}
dim words as string()
words = line.Split(Tokens, StringSplitOptions.RemoveEmptyEntries)

then you would look for words (0) = "faction" and pull words(1).

Mike
 
Thanks a lot for the reply Mike!

The console retrieves this:
Opening Stream... export_descr_buildings.txt
"seleucid"

Which is exactly what I wanted. I guess I didn't fully understand how the
Split function worked, but now I know that it replaces the "Token" characters
with "". The option RemoveEmptyEntries excludes those from the array, leaving
you with what you wanted.

Thanks a lot for your help, it helped me quite a lot!

Cheers,
- RedFox
 
Back
Top