Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing A Strings And Deleting

Status
Not open for further replies.

8u9i

IS-IT--Management
Jul 31, 2007
6
GB
Hi,

I am quite new to C# and I am just wondering if anyone could help me out a little.

Basically if I have a text file and lines like the below:

blah blah blah one blah blah blah blah
blah blah blah two blah blah blah blah
blah blah blah three blah blah blah blah
blah blah blah four blah blah blah blah

I would like to be able to look for the line with, say "three" on it and then delete that line. The only things I done like this before are using the split function.


Thanks for your help in advance.
 
You could do something like this:

Code:
 private string  parse(string myStr, string value)
        {
           
            string[] aLines = myStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            int lines = aLines.Length;
            int i = 0;
            StringBuilder sb = new StringBuilder();
            for (i = 0; i < lines; i++)
            {
                int index = aLines[i].IndexOf(value);
                if (!(index > 0)) 
                {
                    sb.Append(aLines[i]);
                    if (i != lines - 1) { sb.AppendLine(); };
                } 
            }           
            
            return sb.ToString();
        }

Just call the method by passing in the string and search value. For example:

Code:
string testString = "This line has a one \r\n this line has a three";
string testValue = "three";
string result = parse(testString,testValue);

Hope this helps,

-Kevin

Kevin Davie
Consultant
Sogeti USA
 
Just wondering actually... if you can help.

What would be the easiest way to write this back to another file? Using a StreamWriter in the for loop?


Thanks again.
 
Here's what I got so far for reading in the text file, the parsing it, then write it back out.

Doesn't quite work though. Any ideas?

Thanks in advance!!

Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace StringParse
{


    class Program
    {
        public static string strpa(string myStr, string value)
        {

            string[] aLines = myStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            int lines = aLines.Length;
            int i = 0;
            StringBuilder sb = new StringBuilder();
            StreamWriter sbw = new StreamWriter("c:\\test2.txt");
            

            for (i = 0; i < lines; i++)
            {
                int index = aLines[i].IndexOf(value);
                if (!(index > 0))
                {
                    sb.Append(aLines[i]);
                    sbw.Write(aLines[i]);

                    if (i != lines - 1) { 
                        
                        sb.AppendLine();
                        sbw.WriteLine();
                        
                    };
                }
            }
            sbw.Close();
            return sb.ToString();
        }
    

        static void Main(string[] args)
        {

            string outstr;
            string in1;
            string restr;
            
            StreamReader sread = new StreamReader("c:\\test1.txt");
            
            while (sread.ReadLine() != null)
            {
                restr = restr + sread.ReadLine() + "\r\n";
                
            }

            sread.Close();


           

            in1 = "testtest";
            outstr = strpa(restr, in1);
            
            

        }
    }
}
 
I wouldn't recommend using the stream writer inside of the for loop. Instead, I would return the new string as in my above example. Then write the entire string at once to a file via the string writer. I will post an example here in a bit when I get some free time.

-Kevin

Kevin Davie
Consultant
Sogeti USA
 
Thanks, that would be appreciated! I've tried a few ways of messing about with the strings, none of which have either read from the file properly and then written to it properly.

Thanks again.
 
Basically I would do something like this:


Code:
        private void parse()
        {
            string filePath = @"C:\input.txt";            
            string str = fileToString(filePath);
            string output = parse(str, "three");
            stringToFile(output, filePath);
        }
        private string  parse(string myStr, string value)
        {
           
            string[] aLines = myStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            int lines = aLines.Length;
            int i = 0;
            StringBuilder sb = new StringBuilder();
            for (i = 0; i < lines; i++)
            {
                int index = aLines[i].IndexOf(value);
                if (!(index > 0)) 
                {
                    sb.Append(aLines[i]);
                    if (i != lines - 1) { sb.AppendLine(); };
                } 
            }           
            
            return sb.ToString();
        }
        private string fileToString(string file)
        {
            StreamReader sr = new StreamReader(file);        
            string input = sr.ReadToEnd();
            sr.Close();
            return input;
        }
        private void stringToFile(string str, string file)
        {
            StreamWriter sw = new StreamWriter(file);
            sw.Write(str);
            sw.Close();
        }


Kevin Davie
Consultant
Sogeti USA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top