Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8754287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:35:24+00:00 2026-06-13T13:35:24+00:00

I have a question about read a text file, because i dont know if

  • 0

I have a question about read a text file, because i dont know if i’m thinking right. I want to read from specific string to specific character.

My text would look like this:

... 
...
CM_ "Hello, how are you?

Rules: Don't smoke!
      - love others

End";
...
CM_ "Why you?";
...// Many CM_
...

After Splited should look like that:

1. CM_
2. "Hello, how are you?

    Rules: Don't smoke!
      - love others

    End"
3. CM_
4. "Why you?"
... // many CM_

I want to read from "CM_" till ";"

My Code i tried so far:

StreamReader fin = new StreamReader("text.txt");
string tmp = "";
tmp = fin.ReadToEnd();

if (tmp.StartsWith("CM_ ") && tmp.EndWith(";"))
{
var result = tmp.Split(new[] { '"' }).SelectMany((s, i) =>
                    {
                        if (i % 2 == 1) return new[] { s };
                        return s.Split(new[] { ' ', ';' }, StringSplitOptions.RemoveEmptyEntries);
                    }).ToList();
}
foreach (string x in result)
{
   Console.WriteLine(x);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:35:25+00:00Added an answer on June 13, 2026 at 1:35 pm
        static void PRegex()
        {
            using (StreamReader fin = new StreamReader("text.txt"))
            {
                string tmp = fin.ReadToEnd();
    
                var matches = Regex.Matches(tmp, "(CM_) ([^;]*);", RegexOptions.Singleline);
                for (int i = 0; i < matches.Count; i++)
                    if (matches[i].Groups.Count == 3)
                        Console.WriteLine((2 * i + 1).ToString() + ". " + matches[i].Groups[1].Value + "\r\n" + (2 * (i + 1)).ToString() + ". " + matches[i].Groups[2].Value);
            }
    
            Console.ReadLine();
        }
    
        static void PLineByLine()
        {
            using (StreamReader fin = new StreamReader("text.txt"))
            {
                int index = 0;
                string line = null;
                string currentCMBlock = null;
                bool endOfBlock = true;
                while ((line = fin.ReadLine()) != null)
                {
                    bool endOfLine = false;
                    while (!endOfLine)
                    {
                        if (endOfBlock)
                        {
                            int startIndex = line.IndexOf("CM_ ");
                            if (startIndex == -1)
                            {
                                endOfLine = true;
                                continue;
                            }
                            line = line.Substring(startIndex + 4, line.Length - startIndex - 4);
                            endOfBlock = false;
                        }
    
                        if (!endOfBlock)
                        {
                            int startIndex = line.IndexOf(";");
                            if (startIndex == -1)
                            {
                                currentCMBlock += line + "\r\n";
                                endOfLine = true;
                                continue;
                            }
                            currentCMBlock += line.Substring(0, startIndex);
                            if (!string.IsNullOrEmpty(currentCMBlock))
                                Console.WriteLine((++index) + ". CM_\r\n" + (++index) + ". " + currentCMBlock);
                            currentCMBlock = null;
                            line = line.Substring(startIndex + 1, line.Length - startIndex - 1);
                            endOfBlock = true;
                        }
                    }
                }
            }
    
            Console.ReadLine();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some question about WH_MOUSE. From what I've read, by putting the hook
i have question about finding maximum x or y-value in text file. This is
I asked a question about How to read a text file reversely with iterator
I have a question about the C++ visibility attribute. I have read http://gcc.gnu.org/wiki/Visibility and
I have read many question about improving the performance of C++ and C code
Read about the issue in this stackoverflow question . Still have the same issue
I have a text file that contains about 100000 articles. The structure of file
I have a CSV-like text file that has about 1000 lines. Between each record
I'm trying to read from a text file in Netbeans. In the top level
I have this subtle question about virtual memory. In one book I read this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.