如何解析与C#的文本文件 [英] How to parse a text file with C#

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

问题描述

根据文本格式我的意思是更复杂的东西。

By text formatting I meant something more complicated.

起初,我开始从文本文件中手动添加5000行我所要求的这个问题,在我的。项目

At first I began manually adding the 5000 lines from the text file I'm asking this question for,into my project.

该文本文件有5000行不同length.For例如:

The text file has 5000 lines with different length.For example:

1   1	ITEM_ETC_GOLD_01	골드(소)	xxx	xxx	xxx_TT_DESC	0	0	3	3	5	0	180000	3	0	1	0	0	255	1	1	0	0	0	0	0	0	0	0	0	0	-1	0	-1	0	-1	0	-1	0	-1	0	0	0	0	0	0	0	100	0	0	0	xxx	item\etc\drop_ch_money_small.bsr	xxx	xxx	xxx	0	2	0	0	1	0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0	0	0	0	0	0	0	0	0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	1	표현할 골드의 양(param1이상)	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	0	0

1   4	ITEM_ETC_HP_POTION_01	HP 회복 약초	xxx	SN_ITEM_ETC_HP_POTION_01	SN_ITEM_ETC_HP_POTION_01_TT_DESC	0	0	3	3	1	1	180000	3	0	1	1	1	255	3	1	0	0	1	0	60	0	0	0	1	21	-1	0	-1	0	-1	0	-1	0	-1	0	0	0	0	0	0	0	100	0	0	0	xxx	item\etc\drop_ch_bag.bsr	item\etc\hp_potion_01.ddj	xxx	xxx	50	2	0	0	1	0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0	0	0	0	0	0	0	0	0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	120	HP회복양	0	HP회복양(%)	0	MP회복양	0	MP회복양(%)	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	0	0

1   5	ITEM_ETC_HP_POTION_02	HP 회복약 (소)	xxx	SN_ITEM_ETC_HP_POTION_02	SN_ITEM_ETC_HP_POTION_02_TT_DESC	0	0	3	3	1	1	180000	3	0	1	1	1	255	3	1	0	0	1	0	110	0	0	0	2	39	-1	0	-1	0	-1	0	-1	0	-1	0	0	0	0	0	0	0	100	0	0	0	xxx	item\etc\drop_ch_bag.bsr	item\etc\hp_potion_02.ddj	xxx	xxx	50	2	0	0	2	0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0	0	0	0	0	0	0	0	0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	0.0	220	HP회복양	0	HP회복양(%)	0	MP회복양	0	MP회복양(%)	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	-1	xxx	0	0

的第一个字符(1)和第二个字符(1/4/5)之间的文本是。不是一个空白,这是一个tab.There在该文本文件中没有空格

The text between the first character(1) and the second character(1/4/5) is not a whitespace,it's a tab.There's no whitespaces in that text file.

我要什么:

我想获得第二整数(在三线I张贴以上,第二整数是1,4和5)以及在每行表示的路径的中间的字符串(它以item\和结尾。文件扩展名.ddj)

I want to get the second integer(In the three lines I posted above,the second integers are 1,4 and 5) and the string in the middle of each line indicating the path(It starts with "item\" and ends with the file extension ".ddj").

我的问题:

当我谷歌的文本格式化C# - 我得到的是如何打开一个文本文件,以及如何编写C#。我一个文本文件,不知道如何搜索文本file.Also我不能搜索的第一个整数内的文本,因为万一它的一个小的整数像三行我上面贴的,我不会是能够找到corrent位置,因为如1可能会在不同的位置存在。

When I google "Text formatting C#" - all I get is how to open a text file and how to write a text file in C#.I don't know how to search for text inside a text file.Also I can't search for the first integer,because in case its a small integer like in the three lines I posted above,I wont be able to find the corrent location,because for example "1" might exist in a different location.

我的问题:

这将是最好的,如果我写一个程序,将删除任何东西,但我需要什么

It would be the best If I write a program that would delete anything,but what I need.

在我脑海中的另一种方法是直接在文件中搜索,但正如我上面提到的 - 我可能会,如果它太低第二个整数的错误的位置

The other way in my mind is to directly search inside that file,but as I mentioned above - I might get the wrong location of the second integer if its too low.

请提出建议,我不能用手格式化这一切

Please suggest something,I can't format all this by hand.

推荐答案

OK,这里是我们做什么:打开文件,一行行读它,并通过标签拆呢。然后我们抓住第二个整数,并遍历休息寻找路径。

OK, here's what we do: open the file, read it line by line, and split it by tabs. Then we grab the second integer and loop through the rest to find the path.

StreamReader reader = File.OpenText("filename.txt");
string line;
while ((line = reader.ReadLine()) != null) {
    string[] items = line.Split('\t');
    int myInteger = int.Parse(items[1]); // Here's your integer.
    // Now let's find the path.
    string path = null;
    foreach (string item in items) {
        if (item.StartsWith("item\\") && item.EndsWith(".ddj")) {
            path = item;
        }
    }

    // At this point, `myInteger` and `path` contain the values we want
    // for the current line. We can then store those values or print them,
    // or anything else we like.
}

这篇关于如何解析与C#的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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