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 8575975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:49:06+00:00 2026-06-11T19:49:06+00:00

All I have is a file and a string to be searched inside it

  • 0

All I have is a file and a string to be searched inside it

Key
 Value1
 Value2
 Value3

Key1
 Value1

This is the structure of the file. Now, I search for a key and then read all values under it
until I find a newline (or simply a empty line)

I use this algorithm.

var valuelist = new List<string>();
using(var reader = new StreamReader(@"c:\test.txt"))
{
  String a;
  while( (a=reader.ReadLine())!=null)
 {
  if(!a.Equals("Key")) continue;
  while( a == reader.ReadLine() != null) //check whether end of file is not reached.
  {
    if(a.Length == 0) break; //a empty line is reached.hence comeout.
    valuelist.add(a);
  }
  } 
}
  1. I am using “using” because it automatically disposes the “reader” object ? Is my approach right in this case ?

  2. How Can I use a LINQ expression here in this context ?

I tried the following code

var all_lines = File.ReadAllLines(@"C:\test.txt");
 //How to retrieve "Values" for a given key using LINQ ?
  • 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-11T19:49:08+00:00Added an answer on June 11, 2026 at 7:49 pm
    1. The use of using in this context is very appropriate
    2. To retrieve the values for a given key, you can use SkipWhile looking for the key by name, followed by TakeWhile looking for a blank line.

    var list = new List<string>{
        "junk", " a", " b", "", "key", " c", " d", " e", "", "more", "f"
    };
    var vals = list.SkipWhile(s => s != "key").Skip(1).TakeWhile(s => s != "");
    foreach (var s in vals) {
        Console.WriteLine(s);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
I have html-file. I have to replace all text between this: [%anytext%]. As I
Okay, so I have this CSV file of products and all the products have
I have a function that searches for a string inside a text file. I
I have two hashsets like this: HashSet<string> log1 = new HashSet<string>(File.ReadLines(log1.txt)); HashSet<string> log2 =
I have a big ol' .po file containing all the user-facing strings I need
I have file locale/es/LC_MESSAGES/django.mo (and .po), ran makemessages and compilemessages. Definitely all messages are
I have all the class definitions in a header file: ModelModule.h. I have provided
i have a file with the points for all the states. i want to
I have a file with an extension .dbs. All that I know that it's

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.