开发者

How to display part of a line in a .txt file - C#

I'm need of some basic help.

I've got a Folder, in which there is a File.

In the File, there are two lines, and the data in the lines is seperated with a "//".

Example:

There is a folder at location @"C:\ExampleFolder_ABCD\"

In the folder there is a file @"C:\ExampleFolder_ABCD\ExampleFile_ABCD.txt"

In the file there are two lines:

Name_1 // Description_1

Name_2 // Description_2

I need my program to display the first part of each Line, the part before "//" and only this part.

I've done some research, but am counting on some real-time help.

Of course, any help, be good or bad, will be greatly appreciated.

NOTICE : This has nothing to do with homework. It conserns a project of mine, that will help me organise my phonebook.

Lovro Mirnik

If you feel like testing, copy the below code into a newly created Namespace, edit and execute it.

string MainDirectoryPath = @"C:\ExampleFolder_ABCD\"; // Example directory path - Completely random name (100% no overwrite)
string MainFileName = @"C:\ExampleFolder_ABCD\ExampleFile_ABCD.txt"; // Example file path - Completely random name (100% no overwrite)
Directory.CreateDirectory(MainDirectoryPath); // Create the directory.
StreamWriter CreateExampleFile = new StreamWriter(MainFileName); // Create the file.
CreateExampleFile.Close(); // Close the process.
StreamWriter WriteToExampleFile = 开发者_开发技巧File.AppendText(MainFileName); // Append text to the file.
WriteToExampleFile.WriteLine("Name_1 // Description_1"); // This line to append.
WriteToExampleFile.WriteLine("Name_2 // Description_2"); // This line to append.
WriteToExampleFile.Close(); // Close the process.
//
//
// I would like to know how to display both names in a list
// without the Description part of the line.
// Maybe with a command that contains "* // *" ??


Here's some code:

StreamReader Reader = new StreamReader(MainFileName);
            char c = Convert.ToChar(@"/");
            Char[] splitChar = { c, c };
            String Line;
            while (!Reader.EndOfStream)
            {
                Line = Reader.ReadLine();
                String[] Parts;
                Parts = Line.Split(splitChar);
                foreach (string s in Parts)
                {
                    Console.WriteLine(s);
                }
            }
            Reader.Close();
            Console.WriteLine("Done");


I think you'll find all you need here : http://www.dotnetperls.com/string-split

Around the middle there's a bunch of code splitting strings from a text file, you can use it after replacing the split part with something like

Regex.Split(myline, "\\\\")[0]

It should work like a charm.


From your posted example all you would need to do would be to split each line on "\\\\" (you will have to escape the slashes). Take the first result of the split and there you go.


Another variation, using the Split method on the string object:

var result = myString.Split(new char[] {'/','/'})[0]

Just splits the string where it finds "//" into an array. Then you pull back the first element in the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜