C#将文本文件拆分为3列 [英] C# split a text file into 3 columns

查看:103
本文介绍了C#将文本文件拆分为3列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的是一个列表框,用于加载一个文本文件,其中包含从A到Z排序的大约100个项目,我想将它们分成不同的列



第1栏AF

第2栏JR

第3栏SZ



我想知道的是,如果这样是可能的,如果是这样我将如何开始我无法找到任何关于此的信息。



我尝试了什么: < br $> b $ b

我没有尝试太多我不知道从哪里开始或者如果它甚至可能并且我的所有搜索都变空了,因为它是一个非常具体的事实我猜猜这个不寻常的问题?

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?

推荐答案

即使您的要求(如我所评论的那样),我也会考虑一种开始思考的方法。 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


这篇关于C#将文本文件拆分为3列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆