CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2010
    Posts
    23

    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?

  2. #2
    Join Date
    Jan 2009
    Posts
    12

    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.

  3. #3
    Join Date
    Jun 2010
    Posts
    23

    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.

  4. #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;
                }
            }

  5. #5
    Join Date
    Feb 2010
    Posts
    14

    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

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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
  •  





Click Here to Expand Forum to Full Width

Featured