简体   繁体   English

从&#39;string&#39;转换为&#39;System.Collections.Generic.IEnumerable <string>

[英]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 我想将quert转换为字符串,以便我可以对其进行编辑,例如以下代码

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

I can use res[0].Substring and res[0].PadRight many options in text 我可以使用res[0].Substringres[0].PadRight文本中的许多选项

So how can I convert my first code to become string but do same function? 那么,如何将我的第一个代码转换为字符串,但功能相同?

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();

You can simply add .ToArray() at the end : 您只需在末尾添加.ToArray()即可:

    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 . 因此,您可以像query[0].Substringquery[0].PadRight

Is that what you want? 那是你要的吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TreeView:无法从&#39;System.Collections.Generic.IEnumerable转换 <string> &#39;到&#39;System.Collections.Generic.IEnumerable - TreeView: Cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'System.Collections.Generic.IEnumerable 无法从&#39;System.Collections.Generic.IEnumerable转换为System.Collections.Generic.IEnumerable <string> - Cannot convert from 'System.Collections.Generic.IEnumerable to System.Collections.Generic.IEnumerable<string> 无法从“ System.Collections.Generic.IEnumerable”转换为System.Collections.Generic.IEnumerable <string> - cannot convert from 'System.Collections.Generic.IEnumerable' to System.Collections.Generic.IEnumerable<string> 无法从&#39;System.Collections.Generic.IEnumerable转换 <char> &#39;到&#39;字符串&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string' 无法从“System.Collections.Generic.IEnumerable”转换<string> &#39; 到 &#39;T&#39; - cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'T' 转换&#39;System.Collections.Generic.IEnumerable <string> &#39;至&#39;string [] - Convert 'System.Collections.Generic.IEnumerable<string>' to 'string[] 无法从&#39;System.Collections.Generic.IEnumerable转换 <System.Collection.Generic.List<char> &gt;字符串[] - Cannot convert from 'System.Collections.Generic.IEnumerable<System.Collection.Generic.List<char>> to string[] Linq无法从&#39;System.Collections.Generic.IEnumerable转换 <string> &#39;to&#39;string []&#39; - Linq cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'string[]' 收到此错误“参数 2 无法从 'System.Collections.Generic.IEnumerable string ' 转换为 'string '” - Getting this error “Argument 2 cannot convert from 'System.Collections.Generic.IEnumerable string ' to 'string '” 无法从“Microsoft.AspNetCore.Mvc.ActionResult”转换为“System.Collections.Generic.IEnumerable”<string> &#39; - Cannot convert from 'Microsoft.AspNetCore.Mvc.ActionResult' to 'System.Collections.Generic.IEnumerable<string>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM