• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C#文本文件拆分为3列

用户头像
it1352
帮助1

问题说明

What I have is a list box loading a text file with something around 100 items sorted from A to Z and I would like to split them into different columns

Column 1 A-F
Column 2 J-R
Column 3 S-Z

What I would like to know is if this is possible and if so how would I start I'm not able to find any info on this.

What I have tried:

I have not tried much I have no idea where to start or if it's even possible and all my searches are turning up empty due to the fact it's a very specific and I'm guessing uncommon question?

正确答案

#1
即使您的要求(如我所评论的那样),我也会考虑一种开始思考的方法。 100%定义 - 没有更好的要求这样做可能是错的,但可能会帮助你看到森林的木材



假设你有一个字符串数组,那数组是数据文件中的所有行



I'm going to give you a thought on a way to start thinking, even though your requirements (as Ive commented) aren't 100% defined - to do this without better requirements may be wrong, but may help you see the wood for the forest

lets say you have an array of strings, that array being all the lines in your datafile

string path = @"C:\some dir\somefile.txt";
string[] allFileLines = File.ReadLines(path)





是吗?



因此对于第1列,您希望从allFileLines获得匹配的行,其中某些键落在AF范围内..所以,在非常基本的水平,使用LINQ,





yes ?

so for column 1 you want to get matching 'lines' from allFileLines where some 'key' falls in the range A-F .. so, at a very basic level, using LINQ,

var column1Lines = 
  from line in allFileLines
  where (some condition that matches 'line' to key 'A-F')
  select line;





因此重复了第2列JR,第3列SZ。现在,可能有很多方法可以优化这一点,而且,我可以用一点思考返回一个带有字符串键'column1'的字典 - >第1列的行列表,但是,也许这根本不是必需的/ OTT



这个位(某些条件匹配'''到'''键''' ')根据我发布的评论真的变得至关重要 - 它可能是一个正则表达式匹配,一个单独的功能,以'A-F'作为输入并执行你的比赛,或任何东西..



..但是为了让我们能够为您提供解决方案,它确实需要您更好的'规格'/需求定义 - 一旦您完成了,您可以使用LINQ,或者您可以在allFileLines上使用foreach和'分类器'功能,并使用开关将'分类行'添加到第1,2或3列的列表中



and therefore repeated for column2 J-R, column3 S-Z. Now, there may be many ways of optimising this, and, I could with a bit of thought return a dictionary with a string key 'column1' -> list of lines for column1, but, maybe thats simply not required/OTT

This bit (some condition that matches 'line' to key 'A-F') really becomes crucial according to the comment I posted - it could be a Regex Match, a separate function taking 'A-F' as input and performing your match, or anything ..

.. but for us to be able to give you a solution, it really needs a better 'spec'/requirement definition on your part - once you've done that, you could use LINQ, or you could use a foreach on allFileLines with a 'classifier' function and a switch to add the 'classified line' to a list for column 1, 2 or 3

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhccjjjj
系列文章
更多 icon
同类精品
更多 icon
继续加载