0

I need to read all of .txt file and save data to array/list. File looks like this:

row11    row12    row13
row21    row22    row23
row31    row32    row33

between strings are only spaces.

Next I will insert data from array/list<> to mysql, but it is not problem. Thanks.

EDIT: I need insert 3 columns to mysql like .txt file.

4
  • File.ReadLines, String.Split, List<string> or List<yourRowClassData> should be enough. Start coding. Commented Jan 16, 2016 at 18:02
  • Parsing this is a matter of doing read-by-line with a StringReader and splitting on the space, while removing entry entries someLine.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries); Commented Jan 16, 2016 at 18:03
  • @CharlesMager Yes I am sure. It is spaces. Commented Jan 16, 2016 at 18:06
  • @MaximilianGerhardt I never do it. Please can you explain me on some code? Thanks for your willingness. Commented Jan 16, 2016 at 18:09

4 Answers 4

3

Use String.Split(Char[], StringSplitOptions) where the first parameter specifies that you want to split your string using spaces and tabs, and the second parameter specifies that you ignore empty entries (for cases where there are multiple spaces between entries)

Use this code:

var lines = System.IO.File.ReadAllLines(@"D:\test.txt");
var data = new List<List<string>>();
foreach (var line in lines)
{
    var split = line.Split(new[]{' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
    data.Add(split.ToList());
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. Now I have all rows in list, but how can I select column1(row11,row21,row31) from list?
You can convert the List<List<string>> to a 2-dimensional string array (i.e. string[,] or string[][]) if you're more confortable with that using string[][] table = data.Select(a => a.ToArray()).ToArray();, then looping through that with data[i][j] where you hold one parameter constant (either i as the row or j as the column) using a for loop.
Depending on your use case, a 2D array might be the best option. But ff you're using lists, use Linq to access data (see James Ko's answer for more) var firstColumn = data.Select(line => line.First()); var thirdColumn = data.Select(line => line[2]);
0

You can use File.ReadLines() to read the lines from the file, and then Regex.Split() to split each line into multiple strings:

static IEnumerable<String> SplitLines(string path, string splitPattern)
{
    foreach (string line in File.ReadAllLines(path))
        foreach (string part in Regex.Split(line, splitPattern))
            yield return part;
}

To split by white space, you can use the regex pattern \s+:

var individualStrings = SplitLines(@"C:\path\to\file.txt", @"\s+");

You can use the ToList() extension method to convert it to a list:

List<string> individualStrings = SplitLines(@"D:\test\rows.txt", @"\s+").ToList();

Comments

0

As long as there are never spaces in the "values", then a simple line-by line parser will work.

A simple example

var reader = new StreamReader(filePath);

var resultList = new List<List<string>>();

string line;

while ((line = reader.ReadLine()) != null)
{
    var currentValues = new List<string>();

    // You can also use a StringBuilder
    string currentValue = String.Empty;

    foreach (char c in line)
    {
        if (Char.IsWhiteSpace(c))
        {
            if (currentValue.Length > 0)
            {
                currentValues.Add(currentValue);

                currentValue = String.Empty;
            }
            continue;
        }

        currentValue += c;
    }

    resultList.Add(currentValues);
}

Comments

0

Here's a nifty one-liner based off Amadeusz's answer:

var lines = File.ReadAllLines(fileName).Select(l => l.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries)).SelectMany(words => words);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.