如何在C#中按指定位置从文本文件中读取条形码?
0000016011071693266104*014482*3 15301 45 VETRO NOVA BLUVETRO NOVA 蓝色平弹力 115428815150010050 05420 000033 0003
0000072011076993266101*014687*4 15300 45 VETRO NOVA BLUVETRO NOVA 蓝色平弹力 115428815160010030 05430 000032 0007
我有一个文本文件,其中包含许多条码行行,正如您在上面看到的字符串格式是公司代码,其他显示其他内容。
那么如何在 C# 中逐行、逐字符地读取此文本呢?
0000016011071693266104*014482*3 15301 45 VETRO NOVA BLUVETRO NOVA BLUE FLAT STRETCH 115428815150010050 05420 000033 0003
0000072011076993266101*014687*4 15300 45 VETRO NOVA BLUVETRO NOVA BLUE FLAT STRETCH 115428815160010030 05430 000032 0007
I have a text file which includes many barcode codes line by line, and as you see in above string format are company codes and others show other things.
So how can I get read this text line by line and character by character in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要逐行阅读它,您可以使用
StreamReader
- 请参阅 MSDN http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx另一个选项是:
这为您提供了字符串数组中的所有行,您可以使用它们 - 这使用更多内存,但速度更快...参见例如 http:// msdn.microsoft.com/en-us/library/s2tte0y1.aspx
当字符串中有一行时,您可以分割该行,例如:
结果是,您将该行的各个部分放在一个数组并可以访问例如
MyFields[1]
行中的第二个字段 - 请参阅 http://msdn.microsoft.com/en-us/library/b873y76a.aspx编辑 - 根据评论另一个选项:
如果您确切知道您的位置和长度您可以执行以下操作:
有关 MSDN 参考,请参阅 http://msdn.microsoft.com /en-us/library/aka44szs.aspx
For reading it line by line you can use a
StreamReader
- see for example on MSDN http://msdn.microsoft.com/en-us/library/db5x7c0d.aspxAnother option is:
This give you all lines in a string array and you can work with them - this uses more memory but is faster... see for example http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
When have a line in a string you can split that line for example:
The result is that you have the parts of the line in an array and can access for example the second field in the line with
MyFields[1]
- see http://msdn.microsoft.com/en-us/library/b873y76a.aspxEDIT - as per comment another option:
IF you exactly know the positions and lengths of your fields you can do this:
For MSDN reference see http://msdn.microsoft.com/en-us/library/aka44szs.aspx
您可以使用专用于文件和流的 Microsoft 库来打开文件,并使用 Readline()。
然后,您可以使用专用于解析的 Microsoft 库来解析这些行。
您使用 Microsoft 库创建一个正则表达式来检测条形码(不是 borcod...)
,然后丢弃任何与正则表达式不匹配的内容。
然后编译和调试(可以使用Mono)。瞧,您有一个 C# 程序可以解决您的问题。
注意:你绝对不需要“逐个字符”。 Microsoft 库和解析将更容易满足您的简单需求。
You use Microsoft libraries dedicated to files and streams to open a file, and Readline().
Then you use Microsoft libraries dedicated to parsing to parse those lines.
You create, with Microsoft libraries, a regular expression to detect bar codes (not borcod...)
Then you throw away anything that doesn't match your regular expression.
Then you compile and debug (you can use Mono). And voilà, you have a C# program that solves your problem.
Note: you definitely don't need to go "character by character". Microsoft libraries and parsing will be much easier for your simple need.
如果您只想逐行、逐个字符地阅读它,那么这是一个可能的解决方案:
如果您需要知道您所在的行和字符;您可以使用
for
循环来代替:或者在 LINQ 中使用
SelectMany
:现在,就我的观点而言,“逐行”和“逐个字符”执行此操作对我来说似乎是一个艰难的选择。如果您可以告诉我们条形码中的每一位信息到底是什么,我们可以帮助您以这种方式提取它。
If all you are after is reading it line-by-line, and character-by-character, then this is a possible solution:
If you need to know which line and character you are on; you can use a
for
loop instead:Or use
SelectMany
in LINQ:Now, as far as my opinion goes, doing it "line by line" and "character by character" seems like a difficult choice to me. If you can tell us what exactly each bit of information is in the barcode, we could help you extract it that way.