user1477332
user1477332

Reputation: 325

Convert from 'string' to 'System.Collections.Generic.IEnumerable<string>

Here is my code :

var query = System.IO.File.ReadLines(@"c:\temp\mytext.txt")
            .Select(line => line.Split(new string[] { "   " }, StringSplitOptions.RemoveEmptyEntries)[0]);
            System.IO.File.WriteAllLines(@"c:\temp\WriteLines.txt", query);

I want to convert quert to string so I can edit it , like this code for example

string[] res = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

I can use res[0].Substring and res[0].PadRight many options in text

So how can I convert my first code to become string but do same function?

Upvotes: 0

Views: 1221

Answers (2)

Zilberman Rafael
Zilberman Rafael

Reputation: 1451

I'm assuming that your file looks like this (Like grid):

Text11   Text12   Text13   Text14
Text21   Text22   Text23   Text24
Text31   Text32   Text33   Text34

Now you want to split it by cells so:

var query = System.IO.File.ReadLines(@"c:\temp\mytext.txt")
                .Select(line => line.Split(new[] {"   "}, StringSplitOptions.RemoveEmptyEntries)).ToList();

Now when you writing this back to the file you should join the lines:

System.IO.File.WriteAllLines(@"c:\temp\WriteLines.txt", query.Select(line => String.Join("   ", line)));

And when you want to edit cell you can use:

query[row][column].Substring();

Upvotes: 2

har07
har07

Reputation: 89295

You can simply add .ToArray() at the end :

    var query = System.IO.File.ReadLines(@"c:\temp\mytext.txt")
                .Select(line => line.Split(new string[] { "   " }, StringSplitOptions.RemoveEmptyEntries)[0])
                .ToArray();

So you can do like query[0].Substring and query[0].PadRight.

Is that what you want?

Upvotes: 1

Related Questions