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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:04:38+00:00 2026-06-04T08:04:38+00:00

My project is currently spposd to delete all thetext after the — signs, as

  • 0

My project is currently spposd to delete all thetext after the — signs, as it represents a comment.
my code now is deleting all the text in the text in the text file, een before the comment.

here is my code so far:

static void Main( string[] args )
    {
        string line = null;
        string line_to_delete = "--";
        string desktopLocation = Environment.GetFolderPath( Environment.SpecialFolder.Desktop );
        string text = Path.Combine( desktopLocation, "tim3.txt" );
        string file = Path.Combine( desktopLocation, "tim4.txt" );

        using (StreamReader reader = new StreamReader( text ))
        {
            using (StreamWriter writer = new StreamWriter( file ))
            {
                while (( line = reader.ReadLine() ) != null)
                {
                    if (string.Compare( line, line_to_delete ) == 0)
                        File.WriteAllText( file, File.ReadAllText( text ).Replace( line_to_delete, "" ) );
                    continue;
                }
            }

how can i specify that it must only delete the te
Thanks

  • 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-04T08:04:39+00:00Added an answer on June 4, 2026 at 8:04 am

    The problem in your code is the fact that the replace (and this is the only instruction writing to your output file) would happen only when the file contains a line which is exactly equal to “–“.

    Furthermore you don’t need the while loop if you are using WriteAllText and ReadAllText, and you can’t use them anyway since in this way you would only delete the “–” and not everything that comes after that.

    I think that something like this should work:

    using (StreamReader reader = new StreamReader( text ))
    {
        using (StreamWriter writer = new StreamWriter( file ))
        {
            while (( line = reader.ReadLine() ) != null)
            {
                int idx = line.IndexOf(line_to_delete);
                if (idx == 0) // just skip the whole line
                    continue;
                if (idx > 0)
                    writer.WriteLine(line.Substring(0, idx));
                else
                    writer.WriteLine(line);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my project currently we are using All caps as naming convention for constants.
The project currently I'm working has the following code in the view: <%= product.provider.name
In my asp.net project currently i have business logic and and data access code
The project currently has a UIviewController called Dashboard that acts as the main view
Our project currently uses Crystal Reports for Visual Studio 2008. We need to upgrade
I currently have one project that currently contains multiple packages. These packages make up
We've got a C++ Project that currently uses Make on Linux to build. I'd
I'm working on an embedded project that currently uses C in Linux and uClibc.
I am using a web browser in my current project and currently I'm using
I'm using Eclipse Ganymede to create a web application, but the project's currently just

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.