Saturday, 4 August 2012

ASP.NET Spliting .txt File

I've Answered : http://forums.asp.net/t/1830425.aspx
Question:
I have to read text out of a text file and which is having section seperated by "--------(1)-------". My task is to read the text section by section where the number keeps on increasing.


So,


        string filetoread;
        filetoread = Server.MapPath("MahadTECH.txt"); // here is the Path of your .txt file..
        StreamReader FileStreamObj;
        FileStreamObj = File.OpenText(filetoread);
        string readcontents;
        readcontents = FileStreamObj.ReadToEnd(); // Reading .txt file
        //string textdelimiter;
        //textdelimiter = "--------(1)-------";
        
        string SplitPoint = "--------("; // your Spliting Point...
        string ContentReplace = readcontents.Replace("\r\n", "<br>");
        string[] splitout = ContentReplace.Split(SplitPoint.ToCharArray());
                //lblplaintext.Text = (readcontents + "<br>");
        //Response.Write("<br />" + readcontents.Split(nana.ToCharArray()).GetLength(0));
        
        int i;
        for (i = 0; (i < splitout.Length); i++) // Loop for how much Spiliting Point detemining how much Spliting Point you have... it will know automatically by Loop..
        {
            
            //string[] splitout = readcontents.Split("--------(" + i + ")-------");
            //Response.Write("<b>Split </b>" + (i + 1) + (") " + splitout[i] + "<br />"));
            if (splitout[i] != "") // if it will find --------( it will Write other wise Neglect to next array..
            {
                Response.Write(splitout[i] + "<br />");
            }
        }
        FileStreamObj.Close();
:-)
and dont foget to import NameSpace..
using System.IO;
write this Code in you Button Click or any event where ever you want..
Good Luck` hope My code will help you!

For working of this Code.
This is my MahadTECH.txt
--------(1)--------""" IT is coming From MahadTECH.txt"""
Good luck`--------(2)--------Sincerely, 
Mahad Bin Mukhtar--------(3)-------- 
Please remember to --------(4)--------Mark the replies as Answers
if they help & unmark --------(5)--------them if they provide no help.
----------------------------------------------------
and Result is
1)
""" IT is coming From MahadTECH.txt"""
Good luck`
2)
Sincerely, 
Mahad Bin Mukhtar
3)
Please remember to 
4)
Mark the replies as Answers
if they help & unmark 
5)
them if they provide no help.
Hope it will help you and others..

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More