简体   繁体   中英

How to read content in a line of a file.txt

I have a line like this:

114765 * 3 * 659300 * 01 * 17/01/2013 * * 1 * Chuck Norris * Chuck Norris Jr* Owner * 1 * 28/04/1983 * Conjuge * * * 16/1/2013 * 1 * Quadro Social *

As you can see, the content/fields are separted by * they are fields like matriculation, name, dependent's name, category etc... I'd like to know how could I ´read` it, one by one ?

Also I need to reorder this and write a new file.txt, with these content reordered and without the * ... I'm totally lost !
After I read it and remove the * I need to replace them like \\field\\ : \\114765\\\\3\\\\659300\\\\01\\\\17/01/2013\\\\1\\\\Chuck Norris\\ I tried substring() and StreamWriter StreamReader ReadLine but I couldn't do what I need ;\\

Use String.Split to get a string[] of the columns. You can use String.Join to join all parts together to a new string and File.WriteAlltext or File.WriteAllLines to write it back.

After I read it and remove the * I need to replace them like \\field\\:

So you need to replace the * with \\ :

string[] parts = line.Split(new[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
string newLine = string.Join("", parts.Select(p => string.Format("\\{0}\\", p.Trim())));
File.WriteAllText(filePath, newLine);

Demo

Result:

\114765\\3\\659300\\01\\17/01/2013\\\\1\\Chuck Norris\\Chuck Norris Jr\\Owner\\1\\28/04/1983\\Conjuge\\\\\\16/1/2013\\1\\Quadro Social\\\

Update :

If you want to ignore parts that are empty(caused by two consecutive * * in the string), then you can use this code:

var nonEmptyParts = parts
    .Where(p => !string.IsNullOrWhiteSpace(p))
    .Select(p => string.Format("\\{0}\\", p.Trim()));
string newLine = string.Join("", nonEmptyParts);

That'll result in:

\114765\\3\\659300\\01\\17/01/2013\\1\\Chuck Norris\\Chuck Norris Jr\\Owner\\1\\28/04/1983\\Conjuge\\16/1/2013\\1\\Quadro Social\

I'd want to ignore the first 2 strings of the array, you know just remove it, throw it away

That is Enumerable.Skip :

var nonEmptyParts = parts.Skip(2)
    .Where(p => !string.IsNullOrWhiteSpace(p))
    .Select(p => string.Format("\\{0}\\", p.Trim()));

readline is what you need to read one line from the text file. once you have that whole line in a string, you use the Split method.

line.Split('*');

Will give you an array of strings.

Read you file in a line at a time and then use

myLine.split('*');

This will separate the string into an array, splitting it where ever there is a '*'. See http://msdn.microsoft.com/en-us/library/y7h14879.aspx for further details.

When you want to write it back out to another file you should just be able to arrange the items in the array as you want and write them using a StreamWriter, making sure you pass in your new file name. See http://www.dotnetperls.com/streamwriter for an example.

How about this for reordering the lines in the file? Just change the value of fieldToSortBy to order by other numeric fields.

using System.IO;
using System.Linq;

namespace FileSorter
{
    class Program
    {
        static void Main(string[] args)
        {
            var fieldToSortBy = 0;

            //data.txt contains lines like 114765 * 3 * 659300 * 01 * 17/01/2013 * * 1 * Chuck Norris * Chuck Norris Jr* Owner * 1 * 28/04/1983 * Conjuge * * * 16/1/2013 * 1 * Quadro Social *
            var lines = File.ReadAllLines(@"C:\temp\data.txt");
            var splitLines = lines.Select(l => l.Split('*'));
            var orderedLines = splitLines.OrderByDescending(l => int.Parse(l[fieldToSortBy].Trim()));
            var joinedLines = orderedLines.Select(l => string.Join("*", l));
            File.WriteAllLines(@"C:\temp\output.txt", joinedLines);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM