简体   繁体   English

将所有行读取到 string[] 直到找到下一个符号

[英]Read all lines to string[] until next sign is found

I'm trying to read one textfile (one line per entry, separated by two "enums" used as attribute, see example).我正在尝试读取一个文本文件(每个条目一行,由用作属性的两个“枚举”分隔,参见示例)。

[enum1_0][enum2_0]
line
line

[enum1_0][enum2_1]
line
line

[enum1_1][enum2_0]
line
line

[enum1_1][enum2_1]
line
line

I want to create a string[][][] that stores all these strings, so that I later can easily retrieve them using a method like this, but I'm having alot of trouble with the syntax and how to identify the sign attributes in the textfile:我想创建一个string[][][]来存储所有这些字符串,以便我以后可以使用这样的方法轻松检索它们,但是我在语法和如何识别符号属性方面遇到了很多麻烦在文本文件中:

public static string GetRandomString(Enum1 e1, Enum2 e2)
{
    return _strings[(int)e1][(int)e2][_random.Next(0, length)];
}

To clarify: I have a textfile that has one entry per line, where blocks of lines are separated by "[]"-marked 'signs' that correspond to every index of MyEnum1 and MyEnum2.澄清一下:我有一个文本文件,每行有一个条目,其中行块由“[]”标记的“符号”分隔,这些符号对应于 MyEnum1 和 MyEnum2 的每个索引。

I'm trying to read all strings into a single string[], and then sort them into a string[][][] using System.File.IO.ReadAllLines, so that I can later use my function shown above to retrieve them using my enums.我正在尝试将所有字符串读入一个字符串 [],然后使用 System.File.IO.ReadAllLines 将它们排序为一个字符串 [][][],以便我以后可以使用上面显示的函数来检索它们使用我的枚举。

So, after some proper sleep I reconsidered my structure, and this is my solution:因此,经过适当的睡眠后,我重新考虑了我的结构,这是我的解决方案:

static List<GovermentData> _data = new List<GovermentData>();

static string[] _all = File.ReadAllLines(@"Assets/Resources/" + PREFIXES_FILEPATH + ".txt");
static string[] _nations = File.ReadAllLines(@"Assets/Resources/" + NAMES_FILEPATH + ".txt");

public static void Initialize()
{
    EconomicPolicy ep = EconomicPolicy.RadicalCollectivist;
    TechLevel tl = TechLevel.Ancient;

    //iterate all prefixes
    for (int i = 0; i < _all.Length; i++)
    {
        if (_all[i].Length <= 0)
            continue;

        if(_all[i].StartsWith("["))
        {
            string s = _all[i];

            //remove first [ and last ]
            s = s.Remove(0, 1);
            s = s.Remove(s.Length - 1, 1);

            //split on ][
            string[] r = s.Split(new string[] { "][" }, StringSplitOptions.None);

            ep = (EconomicPolicy)Enum.Parse(typeof(EconomicPolicy), r[0]);
            tl = (TechLevel)Enum.Parse(typeof(TechLevel), r[1]);
        }
        else
            _data.Add(new GovermentData(_all[i], ep, tl));
    }
}
public static string GetCivilisationName(EconomicPolicy ep, TechLevel t)
{
    GovermentData[] gd = _data.FindAll(d => d.economicPolicy == ep && d.techLevel == t).ToArray();

    if(gd.Length >= 1)
        return gd[0].name + " of " + _nations[_random.Next(0, _nations.Length)];
    else
        return gd[_random.Next(0, gd.Length)].name + " of " + _nations[_random.Next(0, _nations.Length)];
}

public struct GovermentData
{
    public readonly string name;
    public readonly EconomicPolicy economicPolicy;
    public readonly TechLevel techLevel;

    public GovermentData(string name, EconomicPolicy economicPolicy, TechLevel techLevel)
    {
        this.name = name;
        this.economicPolicy = economicPolicy;
        this.techLevel = techLevel;
    }
}

Thanks for trying to help me despite my own confusion.感谢您在我自己的困惑中尝试帮助我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取文本文件,直到一行包含一些字符串文件,然后再次继续读取下一行,直到遇到另一个字符串 - Read a text file until a line contains some string file, then again keep reading next lines until another string is encountered 获取字符串之后的所有文本,直到字符串下次出现? - Get all text after a string until next occurrence of the string? 当上一行包含字符串时查找所有下一行 - find all next lines when previous line contains a string 需要从 URL 读取流,更改 URI 中的“页面”属性,直到返回“未找到记录”字符串 - Need to read a stream from a URL, changing a "page" attribute in URI until it returns "No records found" string ReadProcessMemory 读取字符串直到 NULL - ReadProcessMemory Read string until NULL TcpClient不返回读取的所有行 - TcpClient not returning all lines read 如何创建一个正则表达式来解析子字符串的所有字符串行,并且只有在找不到子字符串时才返回匹配项? - How can I create a regex that parses all lines of a string for a substring, and returns a match only if the substring is not found? 从TXT文件中读取所有行,并且仅在每行C#的每个FIRST字符串中搜索 - Read all lines from TXT file and search only in every FIRST string of each line C# 在处理当前消息之前不要读取下一条 Kafka 消息? - Don't read next Kafka message until current message is processed? 查找 N 个匹配后的所有字符,直到下一个 N 个匹配 - Find all character after N match until next N match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM