如何从控制台应用程序中的文本文件中提取子字符串内容 [英] How to extract sub-string content from a text file in console application
问题描述
我的输入文本文件格式
my input text file format
SHIPPER/EXPORTER (2) DOCUMENT NO (5)
EXXON MOBIL CORPORATION NAM2998573 NAM2998573
22777 SPRINGWOODS VILLAGE PARKWAY EXPORT REFERENCES (6)
SPRING, TX 77389-1425 USA 5004753 / 6004754
4451690406
CONSIGNEE (3) FORWARDING AGENT - REFERENCES (7)
MANULI DA AMAZONIA E LOGISTICS INC (931839)
INDUSTRIA DE EMBALAGENS LTDA AV. 3050 POST OAK BLVD, STE 444 CHB:
BURITI 3670 BAIRRO DISTRITO HOUSTON, TX 77056 FMC: 025823
INDUSTRIAL 69075-000 MANAUS, AM TEL. NO. 346-802-4008
BRASIL
CNPJ/CPF: 14269557000137
NOTIFY (4) POINT AND COUNTRY OF ORIGIN (8)
MANULI DA AMAZONIA
INDUSTRIA DE EMBALAGENS LTDA AV. UNITED STATES
BURITI 3670 BAIRRO DISTRITO DOMESTIC ROUTING/EXPORT INSTRUCTIONS (9)
INDUSTRIAL 69075-000 MANAUS, A NO SED REQUIRED - X20180206869589
BRASIL CNPJ: 14.269.557/0001-37
注意:我想阅读并提取第一个托运人/出口商子-string values。
在我的程序代码中,当我正在读取出货单出口商时,它的读取文件是否为值,也是导出参考值。
所以这里当我正在阅读托运人出口商的标题名称和值时我想跳过文件号并导出参考标题名称和值
so请给我一个解决方案
i希望以这种格式显示我的输出
SHIPPER /出口商(2)
EXXON MOBIL CORPORATION
22777 SPRINGWOODS VILLAGE PARKWAY
SPRING,TX 77389-1425 USA
我的尝试:
< pre>
static void Main(string [] args)
{
string text = System.IO.File.ReadAllText(@D:\\ \\ _data.txt);
int i = text.IndexOf(Shipper / Exopter);
string shipperexpoter = text.Substring(i + 15,80) ;
System.IO.File.WriteAllText(@D:\\\ output.txt,Shipper / Exopter =+ shipperexpoter);
Console.WriteLine( Shipper / Expoter =+ shipperexpoter);
Console.Read();
Note: i want to read and extract first Shipper/Exporter sub-string values.
in my program code when i am reading shipper exporter values its reading document no and export reference values also.
so here when i am reading shipper exporter header name and values i want to skip document no and export reference header name and values
so please kindly give me a solution for this
i want to show my output like in this format
SHIPPER/EXPORTER (2)
EXXON MOBIL CORPORATION
22777 SPRINGWOODS VILLAGE PARKWAY
SPRING, TX 77389-1425 USA
What I have tried:
<pre>
static void Main(string[] args)
{
string text = System.IO.File.ReadAllText(@"D:\data.txt");
int i = text.IndexOf("Shipper/Exopter");
string shipperexpoter = text.Substring(i+15,80);
System.IO.File.WriteAllText(@"D:\output.txt", "Shipper/Exopter="+shipperexpoter);
Console.WriteLine("Shipper/Expoter =" +shipperexpoter);
Console.Read();
推荐答案
你需要先仔细观察在你的文本文件中,找出左边的东西和右边的东西区别的东西。如果它始终是空格,并且右手的东西总是在同一列中开始,那么这只是一个类似于你显示的那一行的子串工作。如果不是,那么它会变得更加复杂,您需要弄清楚如何正确识别它。不要只是假设它总是从第40列开始,因为它在文本编辑器中显示了这种方式 - 它可能使用选项卡移动它,如果是这样,那么Substring将无法正常工作。
然后停止读取文本字符串,并将其读作行:
You need to start by looking a lot closer at your text file, and work out what distinguishes the stuff on the left from the stuff on the right. If it's always spaces, and the right hand stuff always starts in the same column, then that's just a Substring job similar to the one line you show. If it isn't, then it gets more complex and you need to work out how to identify it properly. Don't just assume that "it always starts in column 40" because it shows that way in your text editor - it may use tabs to "move it across" and if so then Substring won't work correctly.
Then stop reading the text inst a string, and read it as lines:
string[] lines = File.ReadAllLines(@"D:\data.txt");
然后使用循环检查每一行,直到找到以SHIPPER / EXPORTER开头的那一行
然后你可以处理以下行提取地址信息,然后继续查看是否有更多的出口商。
Then use a loop to examine each line until you find one starting with "SHIPPER/EXPORTER"
You can then process the following lines to extract the address info before continuing to see if there are more exporters in there.
这篇关于如何从控制台应用程序中的文本文件中提取子字符串内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!