English 中文(简体)
c# 如何将文本档案中若干字组纳入阵列
原标题:c# how to put several groups of words from textfile into arrays

i 载有这些字句的案文。

PEOPLE
John
0218753458
ENTERPRISE
stock
30%
HOME
Indiana
West Virginia
PEOPLE
Vahn
031245678
ENTERPRISE
Inc
50%
HOME
melbourne
Australia

i 想将这些档案分为几处,将每一组分别分为PEOPLE、ENTERPRISE和HOME。 例如,产出将是

第一部分[0]

 PEOPLE
 John
 0218753458

part[1]

ENTERPISE
stock
30%

页: 1

HOME
Indiana
West Virginia

页: 1

i) 有计划使用

EDIT #1 (thanks @Slade)

string[] part = s.Split(new string[] { "PEOPLE","ENTERPRISE","HOME" }, StringSplitOptions.None);

i can t change the structure. is there any way to keep the HEADER? or better way to do this?

最佳回答

• 采用管理办法,并且因为你想要将分管放在结果中:

string[] tmp = Regex.Split(originalString, @"(PEOPLE|ENTERPRISE|HOME)");

List result = new List();
for(var i = 1; i < tmp.Count() - 1; i += 2) {
    result.Add(tmp[i] + tmp[i+1]);
}

这给你带来了希望的结果。

为什么我会堵塞症状阵列的原因是,截至Reex.NET2.0。 席卷将归还分裂阵列。 我也开始把指数化为1,因为我们想要我们的分类迟到来。

问题回答
s.Split(new string[] {"PEOPLE", "ENTERPRISE", ... }, StringSplitOptions.RemoveEmptyEntries);

如果你想要把头脑活起来,而不是像现在这样,那么最好把你所扼杀的多倍与每一论点分开,并以手法添加头。 例如,你把你的扼杀与人分开,把人推向每个草坪。 然后,每个草坪由水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水、水。

我不想回答你要求做什么,这样,如果你重新确定你在提问中确定的产出,那么,请不予考虑。 否则,我希望这是有益的;

var peopleList = new List<string>();
var enterpriseList = new List<string>();
var homeList = new List<string>();
List<string> workingList = null;

using (var reader = new StreamReader("input.txt"))
{
    string line = reader.ReadLine();
    while (line != null)
    {
        switch (line)
        {
            case "PEOPLE": { workingList = peopleList; } break;
            case "ENTERPRISE": { workingList = enterpriseList; } break;
            case "HOME": { workingList = homeList; } break;

            default: { workingList.Add(line); } break;
        }

        line = reader.ReadLine();
    }
}

根据您的抽样投入,这三份清单如下:

peopleList = { "John", "0218753458", "Vahn", "031245678" }
enterpriseList = { "stock", "30%", "Inc", "50%" }
homeList = { "Indiana", "West Virginia", "melbourne", "Australia" }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...