-
June 14th, 2010, 10:01 AM
#1
How to store text between two words?
Say i have a text file that looks like this...
[System]
beginning
line 1
line 2
line 3
end
[System1]
beginning
line 1
line 2
line 3
end
...etc
how would i be able to collect the text between each 'beginning' and 'end' and store it in a seperate array for each?
-
June 14th, 2010, 02:08 PM
#2
Re: How to store text between two words?
Here's an example of reading a text file:
http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx
as you read each line you can move it to an array.
Also use string compare to check for befinning and end.
http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx
Last edited by pmatah; June 14th, 2010 at 02:11 PM.
-
June 14th, 2010, 02:16 PM
#3
Re: How to store text between two words?
I know how to read text and move it to an array as it reads through each line.
The only thing is i dont know how to have it store each seperate 'snippet'-each part between beginning and end into its own seperate array
I also know how to use string compare to see if it is the beginning or end but the same issue comes up, I dont know how to tell the computer to take the text between the beginning and end and put it into its own seperate array.
-
June 14th, 2010, 03:34 PM
#4
Re: How to store text between two words?
You could read them into a Dictionary that contains a list and then access them by their name
Code:
Dictionary<string, List<string>> settings = new Dictionary<string, List<string>>();
public void Read(TextReader reader)
{
string line;
while ((line=reader.ReadLine())!=null)
{
if (line.Length <= 0)
continue;
List<string> strings;
if (!settings.TryGetValue(line, out strings))
{
strings = new List<string>();
settings.Add(line, strings);
}
Read(reader, strings);
}
}
private void Read(TextReader reader, List<string> strings)
{
string line;
while ((line = reader.ReadLine()) != null)
{
if (string.Compare(line, "beginning") != 0)
continue;
while ((line = reader.ReadLine()) != null && string.Compare(line, "End") != 0)
{
if (line.Length > 0)
strings.Add(line);
}
break;
}
}
-
June 18th, 2010, 06:47 AM
#5
Re: How to store text between two words?
I would probably rethink how this file gets written if you can and change it to a xml format
ie
<system1>some text here</system1>
Then you can use LINQ to access it, or XMLReaders
-
June 18th, 2010, 07:04 AM
#6
Re: How to store text between two words?
The OP does not make it clear but he was looking to parse C# classes from a file containing multiple classes.
Always use [code][/code] tags when posting code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|